Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CoreSNTP]Sntp deserialize response and Sntp_SendTimeRequest cbmc proofs #15

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
6f2017e
Set up litani infra for coreSNTP
gshvang Apr 29, 2021
ec80f3a
Add content to README.md (#9)
aggarw13 Apr 30, 2021
7d07bee
Add Sntp_Init API for higher-layer of coreSNTP (#8)
aggarw13 May 3, 2021
c677873
Merge branch 'main' of https://github.com/FreeRTOS/coreSNTP
gshvang May 4, 2021
6a88aca
Revert "Set up litani infra for coreSNTP"
gshvang May 4, 2021
a4548d5
Sntp_ConvertToUnixTime CBMC PROOF
gshvang May 7, 2021
1581e4e
Merge branch 'main' into cbmc_proofs
gshvang May 7, 2021
7c0b793
removing uncrstify file
gshvang May 7, 2021
e0b8e66
Merge branch 'cbmc_proofs' of https://github.com/FreeRTOS/coreSNTP in…
gshvang May 7, 2021
c8dc9d8
Address Feedback
gshvang May 7, 2021
d6e4457
Allocation changes in poll Interval proof
gshvang May 7, 2021
c1f5b2d
Sntp_Init CBMC PROOF
gshvang May 11, 2021
d6fa78d
Sntp_Init CBMC PROOF
gshvang May 11, 2021
4324e87
Merge branch 'cbmc_proofs' of https://github.com/FreeRTOS/coreSNTP in…
gshvang May 11, 2021
274f287
Addressing Feedback
gshvang May 11, 2021
29bea02
Sntp_DeserializeResponse Proof
gshvang May 11, 2021
9a17a76
ci.yml changes
gshvang May 11, 2021
cef51b4
ci.yml changes
gshvang May 12, 2021
c836944
ci.yml changes
gshvang May 12, 2021
42afe31
ci.yml changes
gshvang May 12, 2021
7e24bb9
Sntp_DeserializeResponse Proof
gshvang May 11, 2021
43e6d46
ci.yml changes
gshvang May 11, 2021
ed062b9
ci.yml changes
gshvang May 12, 2021
4352bf1
ci.yml changes
gshvang May 12, 2021
3bd6a3b
ci.yml changes
gshvang May 12, 2021
9cdc136
Merge branch 'Sntp_DeserializeResponse_Proof' of https://github.com/F…
gshvang May 12, 2021
12ff596
Updating execute permissions on run-cbmc-proofs.py
gshvang May 13, 2021
439d7e1
Refactor calculateClockOffset to avoid overflow issues
aggarw13 May 13, 2021
078efd3
Address CI check failures
aggarw13 May 13, 2021
f4723d9
Address MISRA Rule 20.7 and 10.3 violations
aggarw13 May 13, 2021
773d71f
Fix spellcheck and formattion failures
aggarw13 May 13, 2021
d35dad7
Fix typo in internal function name
aggarw13 May 13, 2021
fcee136
Replace (0U - unsigned integer) expression with 2s complement operati…
aggarw13 May 13, 2021
3240432
More minor hygiene
aggarw13 May 13, 2021
a069f25
Fix spellings
aggarw13 May 13, 2021
c2fc42e
Remove some complexity of refactoring in the code section after elig…
aggarw13 May 13, 2021
52c0399
Encapsulate operations for signed var = subtraction of unsigned integ…
aggarw13 May 13, 2021
9025bf2
Merge remote-tracking branch 'origin/main' into refactor/calculate-cl…
aggarw13 May 13, 2021
0c777b4
More simplification in clock offset calculation function
aggarw13 May 13, 2021
7ad69d4
Rename function to be more expressive
aggarw13 May 13, 2021
ceaf23b
Minor hygiene
aggarw13 May 14, 2021
b4d90ae
Sntp_SendTimeRequest proof
gshvang May 14, 2021
439b68c
UPdating Lexicon.txt
gshvang May 14, 2021
76a938a
UPdating Lexicon.txt
gshvang May 14, 2021
020d59f
Sntp_SendTimeRequest proof
gshvang May 14, 2021
8645138
UPdating Lexicon.txt
gshvang May 14, 2021
4c0208b
UPdating Lexicon.txt
gshvang May 14, 2021
2572ab1
Merging calculate offset changes
gshvang May 14, 2021
7e6ffdd
build fixes
gshvang May 14, 2021
3901b13
sntp_Init proof changes due to Sntp_init function chnanges
gshvang May 14, 2021
76d873d
Sntp_Init function changes applied to SendTimeRequest Proof
gshvang May 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_CLONE_SUBMODULES=ON \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -Wno-error=pedantic -Wno-variadic-macros -DLOGGING_LEVEL_DEBUG=1'
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -Wno-error=pedantic -Wno-variadic-macros -Wno-error=unknown-pragmas -DLOGGING_LEVEL_DEBUG=1'
make -C build/ all
- name: Test
run: |
Expand Down
11 changes: 11 additions & 0 deletions lexicon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ beforelooptime
blocktimems
br
buffersize
bytesorerror
bytesremaining
bytessent
bytestorecv
bytestorecv
bytestosend
bytestosend
cbmc
clienttime
clienttxtime
clockfreqtolerance
clockoffsetsec
Expand Down Expand Up @@ -45,6 +48,7 @@ expectedinterval
expectedtxtime
faqs
feb
firstorderdiff
fracs
fracsinnetorder
fracsinnetorder
Expand All @@ -62,6 +66,7 @@ ifndef
inc
ingroup
inlooptime
int
iot
ip
iso
Expand All @@ -73,6 +78,8 @@ leapsecondinfo
leapversionmode
lsb
misra
networkinterfacereceivestub
networkinterfacesendstub
nist
noleapsecond
noninfringement
Expand Down Expand Up @@ -156,7 +163,10 @@ serveraddr
servernamelen
serverport
serverresponsetimeoutms
servertime
setsystemtimefunc
signedfirstorderdiffrecv
signedfirstorderdiffsend
sizeof
sntp
sntpbuffertoosmall
Expand Down Expand Up @@ -186,6 +196,7 @@ startingpos
struct
sublicense
testbuffer
testclockoffsetcalculation
timebeforeloop
timeserver
transmittime
Expand Down
150 changes: 120 additions & 30 deletions source/core_sntp_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,87 @@ static uint32_t readWordFromNetworkByteOrderMemory( const uint32_t * ptr )
( ( uint32_t ) *( pMemStartByte + 3 ) ) );
}

/**
* @brief Utility to determine whether the passed first order difference value
* between server and client timestamps represents that the server and client
* are within 34 years of each other to be able to calculate the clock-offset
* value according to the NTPv4 specification's on-wire protocol.
*
* @param[in] firstOrderDiff A first-order difference value between the client
* and server.
*
* @note As the SNTP timestamp value wraps around after ~136 years (exactly at
* 7 Feb 2036 6h 28m 16s), the utility logic checks the first order difference
* in both polarities (i.e. as (Server - Client) and (Client - Server) time values )
* to support the edge case when the two timestamps are in different SNTP eras (for
* example, server time is in 2037 and client time is in 2035 ).
*/
static bool isEligibleForClockOffsetCalculation( uint32_t firstOrderDiff )
{
/* Note: The (UINT32_MAX - firstOrderDiff + 1U) expression represents
* 2's complement or negation of value.
* This is done to be compliant with both CBMC and MISRA Rule 10.1.
* CBMC flags overflow for (unsigned int = 0U - positive value) whereas
* MISRA rule forbids use of unary minus operator on unsigned integers. */
return( ( ( firstOrderDiff & CLOCK_OFFSET_FIRST_ORDER_DIFF_OVERFLOW_BITS_MASK ) == 0U ) ||
( ( ( UINT32_MAX - firstOrderDiff + 1U ) & CLOCK_OFFSET_FIRST_ORDER_DIFF_OVERFLOW_BITS_MASK ) == 0U ) );
}

/**
* @brief Utility to perform a safe subtraction operation of unsigned integers and
* return the value as a signed integer. This function returns the effective subtraction
* value as ( @p minuend - @p subtrahend ).
*
* @note This utility provides safe subtraction result that involve the following 2 operations::
* * Safe subtraction between unsigned integers
* AND
* * Safe conversion of unsigned integer to signed integer
*
* @param[in] minuend The value to subtract from.
* @param[in] subtrahend The amount of value to subtract from @p minuend.
*
* @return The calculated signed subtraction value between the unsigned integers.
*/
static int32_t safeSignedSubtraction( uint32_t minuend,
uint32_t subtrahend )
{
int32_t calculatedValue = 0;

/* The correct polarity of subtraction is "minuend - subtrahend"
* but to avoid overflow in subtraction of unsigned integers, we perform
* subtraction in the polarity that generates a positive value. */
bool polarity = ( minuend > subtrahend ) ? true : false;
uint32_t positiveDiff = ( polarity == true ) ? minuend - subtrahend :
subtrahend - minuend;

/* Check whether the difference value cannot be represented as a signed
* integer without some modification.*/
if( positiveDiff > INT32_MAX )
{
/* Perform 2's complement inversion of the value to convert it to a value less
* than INT32_MAX.
* Note: The following expression is used for 2's complement operation to be
* compliant with both CBMC and MISRA Rule 10.1.
* CBMC flags overflow for (unsigned int = 0U - positive value) whereas
* MISRA rule forbids use of unary minus operator on unsigned integers. */
positiveDiff = UINT32_MAX - positiveDiff + 1U;

/* Reverse the state of the polarity as we performed the 2's complement. */
polarity = !polarity;
}

/* Now safely, store the unsigned value as a signed integer. */
calculatedValue = positiveDiff;

/* Restore the difference value to represent subtraction in the polarity of "minuend - subtrahend". */
if( polarity == false )
{
calculatedValue = 0 - calculatedValue;
}

return calculatedValue;
}

/**
* @brief Utility to calculate clock offset of system relative to the
* server using the on-wire protocol specified in the NTPv4 specification.
Expand Down Expand Up @@ -259,50 +340,59 @@ static SntpStatus_t calculateClockOffset( const SntpTimestamp_t * pClientTxTime,
SntpStatus_t status = SntpSuccess;

/* Variable for storing the first-order difference between timestamps. */
uint32_t firstOrderDiff = 0;
uint32_t firstOrderDiffSend = 0;
uint32_t firstOrderDiffRecv = 0;

assert( pClientTxTime != NULL );
assert( pServerRxTime != NULL );
assert( pServerTxTime != NULL );
assert( pClientRxTime != NULL );
assert( pClockOffset != NULL );

/* Calculate a sample first order difference value between the
* server and system timestamps. */
firstOrderDiff = pClientRxTime->seconds - pServerTxTime->seconds;
/* Calculate first order difference values between the server and system timestamps
* to determine whether they are within 34 years of each other. */

/* To avoid overflow issue, we will store only the positive first order differences of
* the timestamps in the unsigned integer now, and later store the values with correct
* subtraction polarity (i.e. "Server Time - Client Time") later. */
firstOrderDiffSend = ( pServerRxTime->seconds >= pClientTxTime->seconds ) ?
( pServerRxTime->seconds - pClientTxTime->seconds ) :
( pClientTxTime->seconds - pServerRxTime->seconds );
firstOrderDiffRecv = ( pServerTxTime->seconds >= pClientRxTime->seconds ) ?
( pServerTxTime->seconds - pClientRxTime->seconds ) :
( pClientRxTime->seconds - pServerTxTime->seconds );

/* Determine from the first order difference if the system time is within
/* Determine from the first order differences if the system time is within
* 34 years of server time to be able to calculate clock offset.
*
* Note: As the SNTP timestamp value wraps around after ~136 years (exactly at
* 7 Feb 2036 6h 28m 16s), the conditional logic checks first order difference
* in both polarities (i.e. as (Server - Client) and (Client - Server) time values )
* to support the edge case when the two timestamps are in different SNTP eras (for
* example, server time is in 2037 and client time is in 2035 ).
*/
if( ( ( firstOrderDiff & CLOCK_OFFSET_FIRST_ORDER_DIFF_OVERFLOW_BITS_MASK )
== 0U ) ||
( ( ( 0U - firstOrderDiff ) & CLOCK_OFFSET_FIRST_ORDER_DIFF_OVERFLOW_BITS_MASK )
== 0U ) )
if( isEligibleForClockOffsetCalculation( firstOrderDiffSend ) &&
isEligibleForClockOffsetCalculation( firstOrderDiffRecv ) )
{
/* Calculate the clock-offset as system time is within 34 years window
* of server time. */
uint32_t firstOrderDiffSend;
uint32_t firstOrderDiffRecv;
uint32_t sumOfFirstOrderDiffs;

/* Perform ( T2 - T1 ) offset calculation of SNTP Request packet path. */
firstOrderDiffSend = pServerRxTime->seconds - pClientTxTime->seconds;

/* Perform ( T3 - T4 ) offset calculation of SNTP Response packet path. */
firstOrderDiffRecv = 0U - firstOrderDiff;

/* Perform second order calculation of using average of the above offsets. */
sumOfFirstOrderDiffs = firstOrderDiffSend + firstOrderDiffRecv;
/* Now that we have validated that system and server times are within 34 years
* of each other, we will prepare for the clock-offset calculation. To calculate
* clock-offset, the first order difference values need to be stored as signed integers
* in the correct polarity of differences according to the formula. */
int32_t signedFirstOrderDiffSend = 0;
int32_t signedFirstOrderDiffRecv = 0;

/* Calculate the first order differences in the correct subtraction direction as
* "Server Time - Client Time" on both SNTP request and SNTP response network paths. */
signedFirstOrderDiffSend = safeSignedSubtraction( pServerRxTime->seconds, pClientTxTime->seconds );
signedFirstOrderDiffRecv = safeSignedSubtraction( pServerTxTime->seconds, pClientRxTime->seconds );

/* We are now sure that each of the first order differences represents the values in
* the correct direction of polarities, i.e.
* signedFirstOrderDiffSend represents (T2 - T1)
* AND
* signedFirstOrderDiffRecv represents (T3 - T4)
*
* We are now safe to complete the calculation of the clock-offset as the average
* of the signed first order difference values.
*/

/* Use division instead of a bit shift to guarantee sign extension
* regardless of compiler implementation. */
*pClockOffset = ( ( int32_t ) sumOfFirstOrderDiffs / 2 );
*pClockOffset = ( ( signedFirstOrderDiffSend + signedFirstOrderDiffRecv ) / 2 );
}
else
{
Expand Down
53 changes: 53 additions & 0 deletions test/cbmc/include/core_sntp_cbmc_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* coreSNTP v1.0.0
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* @file core_sntp_cbmc_state.h
* @brief Allocation and assumption utilities for the SNTP library CBMC proofs.
*/
#ifndef CORE_SNTP_CBMC_STATE_H_
#define CORE_SNTP_CBMC_STATE_H_

#include "core_sntp_client.h"

/* Application defined Network context. */
struct NetworkContext
{
void * networkContext;
};

/* Application defined authentication context. */
struct SntpAuthContext
{
void * authContext;
};

/**
* @brief Allocate a #SntpContext_t object.
*
* @param[in] pContext #SntpContext_t object information.
*
* @return NULL or allocated #SntpContext_t memory.
*/
SntpContext_t * allocateCoreSntpContext( SntpContext_t * pContext );

#endif /* ifndef CORE_SNTP_CBMC_STATE_H_ */
Loading