Debian Codegraph @

src/functions/fs_funcs.sh

raw
1 #!/usr/bin/env bash
2 # shellcheck disable=SC2154
3 # these top lines are moved during build
4
5 # --- FORTIFY_SOURCE subfunctions (start) ---
6 # is FORTIFY_SOURCE supported by libc?
7 FS_libc_check() {
8 echo_message "* FORTIFY_SOURCE support available (libc) : " "" ""
9
10 if [[ "${#FS_chk_func_libc[@]}" != "0" ]]; then
11 echo_message "\033[32mYes\033[m\n" "Yes," " libc_fortify_source='yes' " ', "libc_fortify_source":"yes"'
12 else
13 echo_message "\033[31mNo\033[m\n" "No," " libc_fortify_source='no' " ', "libc_fortify_source":"no"'
14 exit 1
15 fi
16 }
17
18 # was the binary compiled with FORTIFY_SOURCE?
19 FS_binary_check() {
20 echo_message "* Binary compiled with FORTIFY_SOURCE support: " "" "" ""
21
22 for ((FS_elem_functions = 0; FS_elem_functions < ${#FS_functions[@]}; FS_elem_functions++)); do
23 if [[ ${FS_functions[$FS_elem_functions]} =~ _chk$ ]]; then
24 echo_message "\033[32mYes\033[m\n" "Yes\n" " binary_compiled_with_fortify='yes'>\n" ', "binary_compiled_with_fortify":"yes"'
25 return
26 fi
27 done
28 echo_message "\033[31mNo\033[m\n" "No\n" " binary_compiled_with_fortify='no'>\n" ', "binary_compiled_with_fortify":"no"'
29 }
30
31 FS_comparison() {
32 echo_message "\n" "" ""
33 echo_message " ------ EXECUTABLE-FILE ------- . -------- LIBC --------\n" "" "" ""
34 echo_message " Fortifiable library functions | Checked function names\n" "" "" ""
35 echo_message " -------------------------------------------------------\n" "" "" ""
36
37 for ((FS_elem_libc = 0; FS_elem_libc < ${#FS_chk_func_libc[@]}; FS_elem_libc++)); do
38 for ((FS_elem_functions = 0; FS_elem_functions < ${#FS_functions[@]}; FS_elem_functions++)); do
39 FS_tmp_func=${FS_functions[$FS_elem_functions]}
40 FS_tmp_libc=${FS_chk_func_libc[$FS_elem_libc]}
41
42 if [[ ${FS_tmp_func} =~ ^${FS_tmp_libc}$ ]]; then
43 if [[ ${format} == "cli" ]]; then
44 printf " \033[31m%-30s\033[m | __%s%s\n" "${FS_tmp_func}" "${FS_tmp_libc}" "${FS_end}"
45 else
46 if [[ $FS_elem_functions == 0 ]]; then
47 echo_message "" "${FS_tmp_func},${FS_tmp_libc},yes\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='yes' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"yes\" }"
48 elif [[ $FS_elem_functions == $((${#FS_functions[@]} - 1)) ]]; then
49 echo_message "" "${FS_tmp_func},${FS_tmp_libc},yes\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='yes' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"yes\" }"
50 else
51 echo_message "" "${FS_tmp_func},${FS_tmp_libc},yes\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='yes' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"yes\" }"
52 fi
53 fi
54 ((FS_cnt_total++))
55 ((FS_cnt_unchecked++))
56 elif [[ ${FS_tmp_func} =~ ^${FS_tmp_libc}(_chk)$ ]]; then
57 if [[ ${format} == "cli" ]]; then
58 printf " \033[32m%-30s\033[m | __%s%s\n" "${FS_tmp_func}" "${FS_tmp_libc}" "${FS_end}"
59 else
60 if [[ $FS_elem_functions == 0 ]]; then
61 echo_message "" "${FS_tmp_func},${FS_tmp_libc},no\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='no' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"no\" }"
62 elif [[ $FS_elem_functions == $((${#FS_functions[@]} - 1)) ]]; then
63 echo_message "" "${FS_tmp_func},${FS_tmp_libc},no\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='no' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"no\" }"
64 else
65 echo_message "" "${FS_tmp_func},${FS_tmp_libc},no\n" " <function name='${FS_tmp_func}' libc='${FS_tmp_libc}' fortifiable='no' />\n" ", \"function\": { \"name\":\"${FS_tmp_func}\", \"libc\":\"${FS_tmp_libc}\", \"fortifiable\":\"no\" }"
66 fi
67 fi
68 ((FS_cnt_total++))
69 ((FS_cnt_checked++))
70 fi
71
72 done
73 done
74 }
75
76 FS_summary() {
77 echo_message "\n" "" "\n" ""
78 echo_message "SUMMARY:\n\n" "" "" ""
79 echo_message "* Number of checked functions in libc : ${#FS_chk_func_libc[@]}\n" "${#FS_chk_func_libc[@]}," " <stats nb_libc_func='${#FS_chk_func_libc[@]}'" ", \"stats\": { \"nb_libc_func\":\"${#FS_chk_func_libc[@]}\""
80 echo_message "* Total number of library functions in the executable: ${#FS_functions[@]}\n" "${#FS_functions[@]}," " nb_total_func='${#FS_functions[@]}'" ", \"nb_total_func\":\"${#FS_functions[@]}\""
81 echo_message "* Number of Fortifiable functions in the executable : ${FS_cnt_total}\n" "${FS_cnt_total}," " nb_fortifiable_func='${FS_cnt_total}'" ", \"nb_fortifiable_func\":\"${FS_cnt_total}\""
82 echo_message "* Number of checked functions in the executable : \033[32m${FS_cnt_checked}\033[m\n" "${FS_cnt_checked}," " nb_checked_func='${FS_cnt_checked}'" ", \"nb_checked_func\":\"${FS_cnt_checked}\""
83 echo_message "* Number of unchecked functions in the executable : \033[31m${FS_cnt_unchecked}\033[m\n" "${FS_cnt_unchecked}" " nb_unchecked_func='${FS_cnt_unchecked}' />" ", \"nb_unchecked_func\":\"${FS_cnt_unchecked}\" } "
84 echo_message "\n" "\n" "\n" ""
85 }