Skip to content

Commit

Permalink
Fix buffer overrun in secureZeroMemory when using D_InlineAsm_X86_64 …
Browse files Browse the repository at this point in the history
…or D_InlineAsm_X86
  • Loading branch information
n8sh committed Sep 5, 2023
1 parent 4cc4758 commit c7de86f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/crypto/utils.d
Expand Up @@ -43,7 +43,7 @@ void secureZeroMemory(void* p, in size_t length) pure nothrow @nogc
mov RCX, length;
iter:
xor RBX, RBX;
mov [RDX], RBX;
mov [RDX], BL;
inc RDX;
loop iter;
}
Expand All @@ -57,7 +57,7 @@ void secureZeroMemory(void* p, in size_t length) pure nothrow @nogc
mov ECX, length;
iter:
xor EBX, EBX;
mov [EDX], EBX;
mov [EDX], BL;
inc EDX;
loop iter;
}
Expand Down Expand Up @@ -124,3 +124,18 @@ unittest
secureZeroMemory(cast(void[])i2);
assert(i == i2);
}

unittest
{
// Verify that secureZeroMemory doesn't have a buffer overrun.
ubyte[17] array;
array[] = 1;
ubyte[] slice = array[1..$-1];
secureZeroMemory(slice);
// Slice should be 0.
foreach (b; slice)
assert(b == 0);
// Bytes outside slice should be 1.
assert(array[0] == 1);
assert(array[$-1] == 1);
}

0 comments on commit c7de86f

Please sign in to comment.