| 1 | #!/usr/bin/env bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | # You may not use this file except in compliance with the License. |
| 6 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | |
| 15 | FAILED=0 |
| 16 | |
| 17 | ############################################# |
| 18 | # Grep for command line defines without values |
| 19 | ############################################# |
| 20 | EMPTY_DEFINES=$(grep -Eon "\-D[^=]+=?" CMakeLists.txt | grep -v =) |
| 21 | if [ ! -z "${EMPTY_DEFINES}" ]; then |
| 22 | FAILED=1 |
| 23 | printf "\e[1;34mCommand line define is missing value:\e[0m " |
| 24 | printf "Compilers SHOULD set a default value of 1 when no default is given, " |
| 25 | printf "but that behavior is not required by any official spec. Set a value just in case. " |
| 26 | printf "For example: -DS2N_FOO=1 instead of -DS2N_FOO.\n" |
| 27 | printf "Found: \n" |
| 28 | echo "$EMPTY_DEFINES" |
| 29 | fi |
| 30 | |
| 31 | ############################################# |
| 32 | # Grep for bindings methods without C documentation links. |
| 33 | ############################################# |
| 34 | BINDINGS="bindings/rust/extended/s2n-tls/src" |
| 35 | C_APIS=$(grep -rEo "S2N_API( extern)? [^ ]+ [^(]+\(" api | sed -E "s/^.*? \*?(.*?)\(/\1/") |
| 36 | # Sanity checks |
| 37 | echo $C_APIS | grep -q "s2n_error_get_type" || { echo "Not detecting APIs" ; exit 1; } |
| 38 | echo $C_APIS | grep -q "s2n_connection_new" || { echo "Not detecting pointer APIs" ; exit 1; } |
| 39 | echo $C_APIS | grep -q "s2n_config_set_npn" || { echo "Not detecting unstable APIs" ; exit 1; } |
| 40 | KNOWN_MISSES=( |
| 41 | "s2n_errno_location" |
| 42 | "s2n_cert_chain_and_key_get_private_key" |
| 43 | "s2n_config_set_ctx" |
| 44 | "s2n_client_hello_has_extension" |
| 45 | "s2n_async_pkey_op_perform" |
| 46 | "s2n_config_get_client_auth_type" |
| 47 | ) |
| 48 | C_DOCS_FAILED=0 |
| 49 | for api in $C_APIS; do |
| 50 | if [[ "${KNOWN_MISSES[*]}" =~ "$api" ]]; then continue; fi |
| 51 | CALLS=`grep -ro "$api(" $BINDINGS | wc -l` |
| 52 | if [ "$CALLS" == 0 ]; then continue; fi |
| 53 | DOCS=$(grep -ro "///.* \[\`$api\`\]" $BINDINGS | wc -l) |
| 54 | if [ "$DOCS" == 0 ]; then |
| 55 | if [ $C_DOCS_FAILED == 0 ]; then |
| 56 | C_DOCS_FAILED=1 |
| 57 | FAILED=1 |
| 58 | printf "\e[1;34mRust bindings are missing documentation links:\e[0m " |
| 59 | printf "Where possible, the Rust bindings should link to existing documentation. " |
| 60 | printf "Links can be written like \"[\`s2n_connection_new\`]\".\n" |
| 61 | fi |
| 62 | echo "- $api" |
| 63 | fi |
| 64 | done |
| 65 | |
| 66 | ############################################# |
| 67 | # Grep for any instances of raw memcpy() function. s2n code should instead be |
| 68 | # using one of the *_ENSURE_MEMCPY macros. |
| 69 | ############################################# |
| 70 | S2N_FILES_ASSERT_NOT_USING_MEMCPY=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*") |
| 71 | for file in $S2N_FILES_ASSERT_NOT_USING_MEMCPY; do |
| 72 | RESULT_NUM_LINES=`grep 'memcpy(' $file | wc -l` |
| 73 | if [ "${RESULT_NUM_LINES}" != 0 ]; then |
| 74 | echo "Found ${RESULT_NUM_LINES} raw 'memcpy' calls in $file" |
| 75 | FAILED=1 |
| 76 | fi |
| 77 | done |
| 78 | |
| 79 | ############################################# |
| 80 | # Grep for any instances of raw memcmp() function. s2n code should instead be |
| 81 | # using s2n_constant_time_equals() |
| 82 | # |
| 83 | # KNOWN_MEMCMP_USAGE is used to capture all known uses of memcmp and acts as a |
| 84 | # safeguard against any new uses of memcmp. |
| 85 | ############################################# |
| 86 | S2N_FILES_ASSERT_NOT_USING_MEMCMP=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*" -not -path "*/bindings/*") |
| 87 | declare -A KNOWN_MEMCMP_USAGE |
| 88 | KNOWN_MEMCMP_USAGE["$PWD/stuffer/s2n_stuffer_text.c"]=1 |
| 89 | KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_psk.c"]=1 |
| 90 | KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_protocol_preferences.c"]=1 |
| 91 | KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_cipher_suites.c"]=1 |
| 92 | KNOWN_MEMCMP_USAGE["$PWD/utils/s2n_map.c"]=3 |
| 93 | |
| 94 | for file in $S2N_FILES_ASSERT_NOT_USING_MEMCMP; do |
| 95 | # NOTE: this matches on 'memcmp', which will also match comments. However, there |
| 96 | # are no uses of 'memcmp' in comments so we opt for this stricter check. |
| 97 | RESULT_NUM_LINES=`grep -n 'memcmp' $file | wc -l` |
| 98 | |
| 99 | # set default KNOWN_MEMCMP_USAGE value |
| 100 | [ -z "${KNOWN_MEMCMP_USAGE["$file"]}" ] && KNOWN_MEMCMP_USAGE["$file"]="0" |
| 101 | |
| 102 | # check if memcmp usage is 0 or a known value |
| 103 | if [ "${RESULT_NUM_LINES}" != "${KNOWN_MEMCMP_USAGE["$file"]}" ]; then |
| 104 | echo "Expected: ${KNOWN_MEMCMP_USAGE["$file"]} Found: ${RESULT_NUM_LINES} usage of 'memcmp' in $file" |
| 105 | FAILED=1 |
| 106 | fi |
| 107 | done |
| 108 | |
| 109 | ############################################# |
| 110 | # Assert that functions do not return -1 or S2N_ERR* codes directly. |
| 111 | # To indicate failure, functions should use the S2N_ERROR* macros defined |
| 112 | # in s2n_errno.h. |
| 113 | ############################################# |
| 114 | S2N_FILES_ASSERT_RETURN=$(find "$PWD" -type f -name "s2n*.c" -not -path "*/tests/*" -not -path "*/docs/examples/*") |
| 115 | for file in $S2N_FILES_ASSERT_RETURN; do |
| 116 | RESULT_NEGATIVE_ONE=`grep -rn 'return -1;' $file` |
| 117 | RESULT_S2N_ERR=`grep -rn 'return S2N_ERR*' $file` |
| 118 | RESULT_S2N_FAIL=`grep -rn 'return S2N_FAIL*' $file` |
| 119 | RESULT_S2N_RESULT_ERR=`grep -rn 'return S2N_RESULT_ERR*' $file` |
| 120 | |
| 121 | if [ "${#RESULT_NEGATIVE_ONE}" != "0" ]; then |
| 122 | FAILED=1 |
| 123 | printf "\e[1;34mGrep for 'return -1;' check failed in $file:\e[0m\n$RESULT_NEGATIVE_ONE\n\n" |
| 124 | fi |
| 125 | if [ "${#RESULT_S2N_ERR}" != "0" ]; then |
| 126 | FAILED=1 |
| 127 | printf "\e[1;34mGrep for 'return S2N_ERR*' check failed in $file:\e[0m\n$RESULT_S2N_ERR\n\n" |
| 128 | fi |
| 129 | if [ "${#RESULT_S2N_FAIL}" != "0" ]; then |
| 130 | FAILED=1 |
| 131 | printf "\e[1;34mGrep for 'return S2N_FAIL*' check failed in $file:\e[0m\n$RESULT_S2N_FAIL\n\n" |
| 132 | fi |
| 133 | if [ "${#RESULT_S2N_RESULT_ERR}" != "0" ]; then |
| 134 | FAILED=1 |
| 135 | printf "\e[1;34mGrep for 'return S2N_RESULT_ERR*' check failed in $file:\e[0m\n$RESULT_S2N_RESULT_ERR\n\n" |
| 136 | fi |
| 137 | done |
| 138 | |
| 139 | ############################################# |
| 140 | # Detect any array size calculations that are not using the s2n_array_len() function. |
| 141 | ############################################# |
| 142 | S2N_FILES_ARRAY_SIZING_RETURN=$(find "$PWD" -type f -name "s2n*.c" -path "*") |
| 143 | for file in $S2N_FILES_ARRAY_SIZING_RETURN; do |
| 144 | RESULT_ARR_DIV=`grep -Ern 'sizeof\(.*?\) / sizeof\(' $file` |
| 145 | if [ "${#RESULT_ARR_DIV}" != "0" ]; then |
| 146 | FAILED=1 |
| 147 | printf "\e[1;34mUsage of 'sizeof(array) / sizeof(T)' check failed. Use s2n_array_len instead in $file:\e[0m\n$RESULT_ARR_DIV\n\n" |
| 148 | fi |
| 149 | done |
| 150 | |
| 151 | ############################################# |
| 152 | # Detect any suspicious loops not using s2n_array_len(). |
| 153 | # This is not necessarily a problem, but it's been a common source of errors, |
| 154 | # so we should just enforce stricter conventions. |
| 155 | ############################################# |
| 156 | S2N_FILES_WITH_SIZEOF_LOOP=$(find "$PWD" -type f -name "s2n*.c" -path "*") |
| 157 | for file in $S2N_FILES_WITH_SIZEOF_LOOP; do |
| 158 | WITH_QUESTIONABLE_SIZEOF_LOOP=`grep -Ern 'for \(.+; .+ <=? sizeof\(.+\); .+\)' $file | \ |
| 159 | grep -vE '<=? sizeof\(.*bytes\);' | |
| 160 | grep -vE '<=? sizeof\(.*data\);' | |
| 161 | grep -vE '<=? sizeof\(.*u8\);'` |
| 162 | if [ "${#WITH_QUESTIONABLE_SIZEOF_LOOP}" != "0" ]; then |
| 163 | FAILED=1 |
| 164 | printf "\e[1;34mWarning: sizeof is only valid for arrays of chars or uint8_ts. " |
| 165 | printf "Use s2n_array_len for other types, " |
| 166 | printf "or append \"bytes\", \"data\", or \"u8\" to your variable name for clarity.\n" |
| 167 | printf "File: $file:\e[0m\n$WITH_QUESTIONABLE_SIZEOF_LOOP\n\n" |
| 168 | fi |
| 169 | done |
| 170 | |
| 171 | ############################################# |
| 172 | # Assert that all assignments from s2n_stuffer_raw_read() have a |
| 173 | # notnull_check (or similar manual null check) on the same, or next, line. |
| 174 | # The assertion is shallow; this doesn't guarantee that we're doing the |
| 175 | # *correct* null check, just that we are doing *some* null check. |
| 176 | ############################################# |
| 177 | S2N_FILES_ASSERT_NOTNULL_CHECK=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*") |
| 178 | for file in $S2N_FILES_ASSERT_NOTNULL_CHECK; do |
| 179 | while read -r line_one; do |
| 180 | # When called with the -A option, grep uses lines of "--" as delimiters. We ignore them. |
| 181 | if [[ $line_one == "--" ]]; then |
| 182 | continue |
| 183 | fi |
| 184 | |
| 185 | read -r line_two |
| 186 | |
| 187 | # $line_one definitely contains an assignment from s2n_stuffer_raw_read(), |
| 188 | # because that's what we grepped for. So verify that either $line_one or |
| 189 | # $line_two contains a null check. |
| 190 | null_check_regex="(.*(if|ENSURE).*=\ NULL)|(ENSURE_REF)" |
| 191 | if [[ $line_one =~ $null_check_regex ]] || [[ $line_two =~ $null_check_regex ]]; then |
| 192 | # Found a notnull_check |
| 193 | continue |
| 194 | else |
| 195 | FAILED=1 |
| 196 | printf "\e[1;34mFound a call to s2n_stuffer_raw_read without an ENSURE_REF in $file:\e[0m\n$line_one\n\n" |
| 197 | fi |
| 198 | done < <(grep -rnE -A 1 "=\ss2n_stuffer_raw_read\(.*\)" $file) |
| 199 | done |
| 200 | |
| 201 | ############################################# |
| 202 | # Assert that "index" is not a variable name. An "index" function exists in strings.h, and older compilers (<GCC 4.8) |
| 203 | # warn if any local variables called "index" are used because they are considered to shadow that declaration. |
| 204 | ############################################# |
| 205 | S2N_FILES_ASSERT_VARIABLE_NAME_INDEX=$(find "$PWD" -type f -name "s2n*.[ch]") |
| 206 | for file in $S2N_FILES_ASSERT_VARIABLE_NAME_INDEX; do |
| 207 | RESULT_VARIABLE_NAME_INDEX=`gcc -fpreprocessed -dD -E -w $file | grep -v '"' | grep '[\*|,|;|[:space:]]index[;|,|\)|[:space:]]'` |
| 208 | if [ "${#RESULT_VARIABLE_NAME_INDEX}" != "0" ]; then |
| 209 | FAILED=1 |
| 210 | printf "\e[1;34mGrep for variable name 'index' check failed in $file:\e[0m\n$RESULT_VARIABLE_NAME_INDEX\n\n" |
| 211 | fi |
| 212 | done |
| 213 | |
| 214 | ############################################# |
| 215 | # Assert that we didn't accidentally add an extra semicolon when ending a line. |
| 216 | # This also errors when there is whitespace in-between semicolons, as that is an empty |
| 217 | # statement and usually not purposeful. |
| 218 | ############################################# |
| 219 | S2N_FILES_ASSERT_DOUBLE_SEMICOLON=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/bindings/*") |
| 220 | for file in $S2N_FILES_ASSERT_DOUBLE_SEMICOLON; do |
| 221 | RESULT_DOUBLE_SEMICOLON=`grep -Ern ';[[:space:]]*;' $file` |
| 222 | if [ "${#RESULT_DOUBLE_SEMICOLON}" != "0" ]; then |
| 223 | FAILED=1 |
| 224 | printf "\e[1;34mFound a double semicolon in $file:\e[0m\n$RESULT_DOUBLE_SEMICOLON\n\n" |
| 225 | fi |
| 226 | done |
| 227 | |
| 228 | ############################################# |
| 229 | # Assert that we don't call malloc directly outside of utils/s2n_mem.c or tests. |
| 230 | # All other allocations must use s2n_alloc or s2n_realloc. |
| 231 | ############################################# |
| 232 | S2N_FILES_ASSERT_MALLOC=$(find "$PWD" -type f -name "s2n*.c" \ |
| 233 | -not -path "*/utils/s2n_mem.c" \ |
| 234 | -not -path "*/tests/*" \ |
| 235 | -not -path "*/bin/*") |
| 236 | for file in $S2N_FILES_ASSERT_MALLOC; do |
| 237 | RESULT_MALLOC=`grep -n "= malloc(" $file` |
| 238 | if [ "${#RESULT_MALLOC}" != "0" ]; then |
| 239 | FAILED=1 |
| 240 | printf "\e[1;34mFound malloc in $file:\e[0m\n$RESULT_MALLOC\n\n" |
| 241 | fi |
| 242 | done |
| 243 | |
| 244 | ############################################# |
| 245 | ## Assert that there are no new uses of S2N_ERROR_IF |
| 246 | # TODO add crypto, tls (see https://github.com/aws/s2n-tls/issues/2635) |
| 247 | ############################################# |
| 248 | S2N_ERROR_IF_FREE="bin error scram stuffer utils tests" |
| 249 | for dir in $S2N_ERROR_IF_FREE; do |
| 250 | files=$(find "$dir" -type f -name "*.c" -path "*") |
| 251 | for file in $files; do |
| 252 | result=`grep -Ern 'S2N_ERROR_IF' $file` |
| 253 | if [ "${#result}" != "0" ]; then |
| 254 | FAILED=1 |
| 255 | printf "\e[1;34mUsage of 'S2N_ERROR_IF' check failed. Use 'POSIX_ENSURE' instead in $file:\e[0m\n$result\n\n" |
| 256 | fi |
| 257 | done |
| 258 | done |
| 259 | |
| 260 | ############################################# |
| 261 | ## Assert all ".[c|h]" source files have the correct file mode. |
| 262 | ############################################# |
| 263 | S2N_FILES_WITH_WRONG_MODE=$(find "$PWD" \( -name '*.c' -o -name '*.h' \) -a \! -perm 644) |
| 264 | if [[ -n $S2N_FILES_WITH_WRONG_MODE ]]; then |
| 265 | for file in $S2N_FILES_WITH_WRONG_MODE; do |
| 266 | FAILED=1 |
| 267 | printf "\\033[31;1mFile mode of %s is not 644.\\033[0m\\n" "$file" |
| 268 | done |
| 269 | printf "\\033[31;1mPlease run \`find s2n-tls/ -name '*.c' -o -name '*.h' -exec chmod 644 {} \\;\` to fix all file modes.\\033[0m\\n" |
| 270 | fi |
| 271 | |
| 272 | ############################################# |
| 273 | ## Assert "extern" is not added to function declarations unnecessarily. |
| 274 | ############################################# |
| 275 | S2N_UNNECESSARY_EXTERNS=$(find "$PWD" -type f -name "s2n*.[h]" \! -path "*/api/*" \! -path "*/bindings/*" \ |
| 276 | -exec grep -RE "extern (.*?) (.*?)\(" {} +) |
| 277 | if [[ -n $S2N_UNNECESSARY_EXTERNS ]]; then |
| 278 | FAILED=1 |
| 279 | printf "\e[1;34mFound unnecessary 'extern' in function declaration:\e[0m\n" |
| 280 | printf "$S2N_UNNECESSARY_EXTERNS\n\n" |
| 281 | fi |
| 282 | |
| 283 | ############################################# |
| 284 | # Assert ENSURE/GUARD have a valid error code |
| 285 | ############################################# |
| 286 | S2N_ENSURE_WITH_INVALID_ERROR_CODE=$(find "$PWD" -type f -name "s2n*.c" -path "*" \! -path "*/tests/*" \ |
| 287 | -exec grep -RE "(ENSURE|GUARD_OSSL)\(.*?, .*?);" {} + | grep -v "S2N_ERR_") |
| 288 | if [[ -n $S2N_ENSURE_WITH_INVALID_ERROR_CODE ]]; then |
| 289 | FAILED=1 |
| 290 | printf "\e[1;34mENSURE and GUARD_OSSL require a valid error code from errors/s2n_errno.h:\e[0m\n" |
| 291 | printf "$S2N_ENSURE_WITH_INVALID_ERROR_CODE\n\n" |
| 292 | fi |
| 293 | |
| 294 | ############################################# |
| 295 | # REPORT FINAL RESULTS |
| 296 | ############################################# |
| 297 | if [ $FAILED == 1 ]; then |
| 298 | printf "\\033[31;1mFAILED Grep For Simple Mistakes check\\033[0m\\n" |
| 299 | exit -1 |
| 300 | else |
| 301 | printf "\\033[32;1mPASSED Grep For Simple Mistakes check\\033[0m\\n" |
| 302 | fi |