c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 1) #! /bin/bash
b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 2) # SPDX-License-Identifier: GPL-2.0
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 3) # (c) 2015, Quentin Casasnovas <quentin.casasnovas@oracle.com>
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 4)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 5) obj=$1
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 6)
b285437d1d929 (Randy Dunlap 2021-09-07 20:00:59 -0700 7) file ${obj} | grep -q ELF || (echo "${obj} is not an ELF file." 1>&2 ; exit 0)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 8)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 9) # Bail out early if there isn't an __ex_table section in this object file.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 10) objdump -hj __ex_table ${obj} 2> /dev/null > /dev/null
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 11) [ $? -ne 0 ] && exit 0
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 12)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 13) white_list=.text,.fixup
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 14)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 15) suspicious_relocs=$(objdump -rj __ex_table ${obj} | tail -n +6 |
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 16) grep -v $(eval echo -e{${white_list}}) | awk '{print $3}')
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 17)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 18) # No suspicious relocs in __ex_table, jobs a good'un
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 19) [ -z "${suspicious_relocs}" ] && exit 0
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 20)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 21)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 22) # After this point, something is seriously wrong since we just found out we
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 23) # have some relocations in __ex_table which point to sections which aren't
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 24) # white listed. If you're adding a new section in the Linux kernel, and
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 25) # you're expecting this section to contain code which can fault (i.e. the
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 26) # __ex_table relocation to your new section is expected), simply add your
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 27) # new section to the white_list variable above. If not, you're probably
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 28) # doing something wrong and the rest of this code is just trying to print
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 29) # you more information about it.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 30)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 31) function find_section_offset_from_symbol()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 32) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 33) eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /')
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 34)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 35) # addr2line takes addresses in hexadecimal...
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 36) section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) )
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 37) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 38)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 39) function find_symbol_and_offset_from_reloc()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 40) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 41) # Extract symbol and offset from the objdump output
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 42) eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/')
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 43)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 44) # When the relocation points to the begining of a symbol or section, it
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 45) # won't print the offset since it is zero.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 46) if [ -z "${symbol_offset}" ]; then
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 47) symbol_offset=0x0
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 48) fi
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 49) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 50)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 51) function find_alt_replacement_target()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 52) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 53) # The target of the .altinstr_replacement is the relocation just before
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 54) # the .altinstr_replacement one.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 55) eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' |
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 56) sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/')
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 57) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 58)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 59) function handle_alt_replacement_reloc()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 60) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 61) # This will define alt_target_section and alt_target_section_offset
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 62) find_alt_replacement_target ${section} ${section_offset}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 63)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 64) echo "Error: found a reference to .altinstr_replacement in __ex_table:"
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 65) addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}'
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 66)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 67) error=true
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 68) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 69)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 70) function is_executable_section()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 71) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 72) objdump -hwj ${section} ${obj} | grep -q CODE
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 73) return $?
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 74) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 75)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 76) function handle_suspicious_generic_reloc()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 77) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 78) if is_executable_section ${section}; then
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 79) # We've got a relocation to a non white listed _executable_
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 80) # section, print a warning so the developper adds the section to
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 81) # the white list or fix his code. We try to pretty-print the file
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 82) # and line number where that relocation was added.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 83) echo "Warning: found a reference to section \"${section}\" in __ex_table:"
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 84) addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}'
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 85) else
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 86) # Something is definitively wrong here since we've got a relocation
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 87) # to a non-executable section, there's no way this would ever be
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 88) # running in the kernel.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 89) echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}"
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 90) error=true
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 91) fi
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 92) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 93)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 94) function handle_suspicious_reloc()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 95) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 96) case "${section}" in
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 97) ".altinstr_replacement")
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 98) handle_alt_replacement_reloc ${section} ${section_offset}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 99) ;;
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 100) *)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 101) handle_suspicious_generic_reloc ${section} ${section_offset}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 102) ;;
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 103) esac
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 104) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 105)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 106) function diagnose()
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 107) {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 108)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 109) for reloc in ${suspicious_relocs}; do
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 110) # Let's find out where the target of the relocation in __ex_table
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 111) # is, this will define ${symbol} and ${symbol_offset}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 112) find_symbol_and_offset_from_reloc ${reloc}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 113)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 114) # When there's a global symbol at the place of the relocation,
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 115) # objdump will use it instead of giving us a section+offset, so
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 116) # let's find out which section is this symbol in and the total
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 117) # offset withing that section.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 118) find_section_offset_from_symbol ${symbol} ${symbol_offset}
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 119)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 120) # In this case objdump was presenting us with a reloc to a symbol
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 121) # rather than a section. Now that we've got the actual section,
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 122) # we can skip it if it's in the white_list.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 123) if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 124) continue;
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 125) fi
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 126)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 127) # Will either print a warning if the relocation happens to be in a
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 128) # section we do not know but has executable bit set, or error out.
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 129) handle_suspicious_reloc
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 130) done
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 131) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 132)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 133) function check_debug_info() {
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 134) objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null ||
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 135) echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 136) "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output."
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 137) }
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 138)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 139) check_debug_info
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 140)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 141) diagnose
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 142)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 143) if [ "${error}" ]; then
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 144) exit 1
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 145) fi
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 146)
c31e4b832f124 (Quentin Casasnovas 2015-04-13 20:44:04 +0930 147) exit 0