Skip to content

Commit 61d9ea8

Browse files
committed
fix(s2): Fixup Support secure memset memset_s (for MacOS clang-17)
Previous change was not tested properly, I have to consider enabling macos CI, to prevent those MacOS fixups. Origin: SiliconLabsSoftware/z-wave-engine-application-layer#95 Relate-to: SiliconLabsSoftware#139 Forwarded: SiliconLabsSoftware#139 Signed-off-by: Philippe Coval <[email protected]>
1 parent 4ccac87 commit 61d9ea8

File tree

1 file changed

+34
-6
lines changed
  • applications/zpc/components/zwave/zwave_transports/s2/libs/zw-libs2/protocol

1 file changed

+34
-6
lines changed

applications/zpc/components/zwave/zwave_transports/s2/libs/zw-libs2/protocol/S2.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#include "s2_protocol.h"
1111
#include "s2_classcmd.h"
1212
#include "../inclusion/s2_inclusion_internal.h"
13-
#include<string.h>
13+
14+
#include <assert.h>
15+
#include <string.h>
16+
1417
#include "ccm.h"
1518
#include "aes_cmac.h"
1619
#include "nextnonce.h"
@@ -1282,13 +1285,25 @@ S2_init_ctx(uint32_t home)
12821285

12831286
// Erase sensitive memory safely
12841287
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
1285-
memset_explicit(ctx, 0, sizeof(struct S2));
1286-
#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12
1288+
void *result = memset_explicit(ctx, 0, sizeof(struct S2));
1289+
assert(result == ctx);
1290+
if (result != ctx) {
1291+
return (NULL);
1292+
}
1293+
#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12
12871294
explicit_bzero(ctx, sizeof(struct S2));
1288-
#elif defined(__APPLE__) // for MacOS
1289-
memset_s(ctx, 0, sizeof(struct S2));
1295+
#elif defined(__APPLE__) // for MacOS
1296+
errno_t result = memset_s(ctx, sizeof(struct S2), 0, sizeof(struct S2));
1297+
assert(result == 0);
1298+
if (result != 0) {
1299+
return (NULL);
1300+
}
12901301
#else
1291-
memset(ctx, 0, sizeof(struct S2)); //NOSONAR: Fallback option
1302+
void *result = memset(ctx, 0, sizeof(struct S2)); //NOSONAR: Fallback option
1303+
assert(result == ctx);
1304+
if (result != ctx) {
1305+
return (NULL);
1306+
}
12921307
#endif
12931308

12941309
ctx->my_home_id = home;
@@ -1339,7 +1354,20 @@ void
13391354
S2_destroy(struct S2* p_context)
13401355
{
13411356
CTX_DEF
1357+
// Erase sensitive memory safely
1358+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
1359+
void *result = memset_explicit(ctxt, 0, sizeof(struct S2));
1360+
assert(result == ctxt);
1361+
#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12
13421362
explicit_bzero(ctxt, sizeof(struct S2));
1363+
#elif defined(__APPLE__) // for MacOS
1364+
errno_t result = memset_s(ctxt, sizeof(struct S2), 0, sizeof(struct S2));
1365+
assert(result == 0);
1366+
#else
1367+
void *result = memset(ctxt, 0, sizeof(struct S2)); //NOSONAR: Fallback option
1368+
assert(result == ctxt);
1369+
#endif
1370+
13431371
#ifndef SINGLE_CONTEXT
13441372
free(ctxt);
13451373
#endif

0 commit comments

Comments
 (0)