#!/usr/bin/env bash # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. # A copy of the License is located at # # http://aws.amazon.com/apache2.0 # # or in the "license" file accompanying this file. This file is distributed # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either # express or implied. See the License for the specific language governing # permissions and limitations under the License. FAILED=0 ############################################# # Grep for command line defines without values ############################################# EMPTY_DEFINES=$(grep -Eon "\-D[^=]+=?" CMakeLists.txt | grep -v =) if [ ! -z "${EMPTY_DEFINES}" ]; then FAILED=1 printf "\e[1;34mCommand line define is missing value:\e[0m " printf "Compilers SHOULD set a default value of 1 when no default is given, " printf "but that behavior is not required by any official spec. Set a value just in case. " printf "For example: -DS2N_FOO=1 instead of -DS2N_FOO.\n" printf "Found: \n" echo "$EMPTY_DEFINES" fi ############################################# # Grep for bindings methods without C documentation links. ############################################# BINDINGS="bindings/rust/extended/s2n-tls/src" C_APIS=$(grep -rEo "S2N_API( extern)? [^ ]+ [^(]+\(" api | sed -E "s/^.*? \*?(.*?)\(/\1/") # Sanity checks echo $C_APIS | grep -q "s2n_error_get_type" || { echo "Not detecting APIs" ; exit 1; } echo $C_APIS | grep -q "s2n_connection_new" || { echo "Not detecting pointer APIs" ; exit 1; } echo $C_APIS | grep -q "s2n_config_set_npn" || { echo "Not detecting unstable APIs" ; exit 1; } KNOWN_MISSES=( "s2n_errno_location" "s2n_cert_chain_and_key_get_private_key" "s2n_config_set_ctx" "s2n_client_hello_has_extension" "s2n_async_pkey_op_perform" "s2n_config_get_client_auth_type" ) C_DOCS_FAILED=0 for api in $C_APIS; do if [[ "${KNOWN_MISSES[*]}" =~ "$api" ]]; then continue; fi CALLS=`grep -ro "$api(" $BINDINGS | wc -l` if [ "$CALLS" == 0 ]; then continue; fi DOCS=$(grep -ro "///.* \[\`$api\`\]" $BINDINGS | wc -l) if [ "$DOCS" == 0 ]; then if [ $C_DOCS_FAILED == 0 ]; then C_DOCS_FAILED=1 FAILED=1 printf "\e[1;34mRust bindings are missing documentation links:\e[0m " printf "Where possible, the Rust bindings should link to existing documentation. " printf "Links can be written like \"[\`s2n_connection_new\`]\".\n" fi echo "- $api" fi done ############################################# # Grep for any instances of raw memcpy() function. s2n code should instead be # using one of the *_ENSURE_MEMCPY macros. ############################################# S2N_FILES_ASSERT_NOT_USING_MEMCPY=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*") for file in $S2N_FILES_ASSERT_NOT_USING_MEMCPY; do RESULT_NUM_LINES=`grep 'memcpy(' $file | wc -l` if [ "${RESULT_NUM_LINES}" != 0 ]; then echo "Found ${RESULT_NUM_LINES} raw 'memcpy' calls in $file" FAILED=1 fi done ############################################# # Grep for any instances of raw memcmp() function. s2n code should instead be # using s2n_constant_time_equals() # # KNOWN_MEMCMP_USAGE is used to capture all known uses of memcmp and acts as a # safeguard against any new uses of memcmp. ############################################# S2N_FILES_ASSERT_NOT_USING_MEMCMP=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*" -not -path "*/bindings/*") declare -A KNOWN_MEMCMP_USAGE KNOWN_MEMCMP_USAGE["$PWD/stuffer/s2n_stuffer_text.c"]=1 KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_psk.c"]=1 KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_protocol_preferences.c"]=1 KNOWN_MEMCMP_USAGE["$PWD/tls/s2n_cipher_suites.c"]=1 KNOWN_MEMCMP_USAGE["$PWD/utils/s2n_map.c"]=3 for file in $S2N_FILES_ASSERT_NOT_USING_MEMCMP; do # NOTE: this matches on 'memcmp', which will also match comments. However, there # are no uses of 'memcmp' in comments so we opt for this stricter check. RESULT_NUM_LINES=`grep -n 'memcmp' $file | wc -l` # set default KNOWN_MEMCMP_USAGE value [ -z "${KNOWN_MEMCMP_USAGE["$file"]}" ] && KNOWN_MEMCMP_USAGE["$file"]="0" # check if memcmp usage is 0 or a known value if [ "${RESULT_NUM_LINES}" != "${KNOWN_MEMCMP_USAGE["$file"]}" ]; then echo "Expected: ${KNOWN_MEMCMP_USAGE["$file"]} Found: ${RESULT_NUM_LINES} usage of 'memcmp' in $file" FAILED=1 fi done ############################################# # Assert that functions do not return -1 or S2N_ERR* codes directly. # To indicate failure, functions should use the S2N_ERROR* macros defined # in s2n_errno.h. ############################################# S2N_FILES_ASSERT_RETURN=$(find "$PWD" -type f -name "s2n*.c" -not -path "*/tests/*" -not -path "*/docs/examples/*") for file in $S2N_FILES_ASSERT_RETURN; do RESULT_NEGATIVE_ONE=`grep -rn 'return -1;' $file` RESULT_S2N_ERR=`grep -rn 'return S2N_ERR*' $file` RESULT_S2N_FAIL=`grep -rn 'return S2N_FAIL*' $file` RESULT_S2N_RESULT_ERR=`grep -rn 'return S2N_RESULT_ERR*' $file` if [ "${#RESULT_NEGATIVE_ONE}" != "0" ]; then FAILED=1 printf "\e[1;34mGrep for 'return -1;' check failed in $file:\e[0m\n$RESULT_NEGATIVE_ONE\n\n" fi if [ "${#RESULT_S2N_ERR}" != "0" ]; then FAILED=1 printf "\e[1;34mGrep for 'return S2N_ERR*' check failed in $file:\e[0m\n$RESULT_S2N_ERR\n\n" fi if [ "${#RESULT_S2N_FAIL}" != "0" ]; then FAILED=1 printf "\e[1;34mGrep for 'return S2N_FAIL*' check failed in $file:\e[0m\n$RESULT_S2N_FAIL\n\n" fi if [ "${#RESULT_S2N_RESULT_ERR}" != "0" ]; then FAILED=1 printf "\e[1;34mGrep for 'return S2N_RESULT_ERR*' check failed in $file:\e[0m\n$RESULT_S2N_RESULT_ERR\n\n" fi done ############################################# # Detect any array size calculations that are not using the s2n_array_len() function. ############################################# S2N_FILES_ARRAY_SIZING_RETURN=$(find "$PWD" -type f -name "s2n*.c" -path "*") for file in $S2N_FILES_ARRAY_SIZING_RETURN; do RESULT_ARR_DIV=`grep -Ern 'sizeof\(.*?\) / sizeof\(' $file` if [ "${#RESULT_ARR_DIV}" != "0" ]; then FAILED=1 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" fi done ############################################# # Detect any suspicious loops not using s2n_array_len(). # This is not necessarily a problem, but it's been a common source of errors, # so we should just enforce stricter conventions. ############################################# S2N_FILES_WITH_SIZEOF_LOOP=$(find "$PWD" -type f -name "s2n*.c" -path "*") for file in $S2N_FILES_WITH_SIZEOF_LOOP; do WITH_QUESTIONABLE_SIZEOF_LOOP=`grep -Ern 'for \(.+; .+ <=? sizeof\(.+\); .+\)' $file | \ grep -vE '<=? sizeof\(.*bytes\);' | grep -vE '<=? sizeof\(.*data\);' | grep -vE '<=? sizeof\(.*u8\);'` if [ "${#WITH_QUESTIONABLE_SIZEOF_LOOP}" != "0" ]; then FAILED=1 printf "\e[1;34mWarning: sizeof is only valid for arrays of chars or uint8_ts. " printf "Use s2n_array_len for other types, " printf "or append \"bytes\", \"data\", or \"u8\" to your variable name for clarity.\n" printf "File: $file:\e[0m\n$WITH_QUESTIONABLE_SIZEOF_LOOP\n\n" fi done ############################################# # Assert that all assignments from s2n_stuffer_raw_read() have a # notnull_check (or similar manual null check) on the same, or next, line. # The assertion is shallow; this doesn't guarantee that we're doing the # *correct* null check, just that we are doing *some* null check. ############################################# S2N_FILES_ASSERT_NOTNULL_CHECK=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*") for file in $S2N_FILES_ASSERT_NOTNULL_CHECK; do while read -r line_one; do # When called with the -A option, grep uses lines of "--" as delimiters. We ignore them. if [[ $line_one == "--" ]]; then continue fi read -r line_two # $line_one definitely contains an assignment from s2n_stuffer_raw_read(), # because that's what we grepped for. So verify that either $line_one or # $line_two contains a null check. null_check_regex="(.*(if|ENSURE).*=\ NULL)|(ENSURE_REF)" if [[ $line_one =~ $null_check_regex ]] || [[ $line_two =~ $null_check_regex ]]; then # Found a notnull_check continue else FAILED=1 printf "\e[1;34mFound a call to s2n_stuffer_raw_read without an ENSURE_REF in $file:\e[0m\n$line_one\n\n" fi done < <(grep -rnE -A 1 "=\ss2n_stuffer_raw_read\(.*\)" $file) done ############################################# # Assert that "index" is not a variable name. An "index" function exists in strings.h, and older compilers (