Skip to content

Commit

Permalink
Merge pull request #32 from n8sh/fix-secureZeroMemory-asm-buffer-overrun
Browse files Browse the repository at this point in the history
Fix buffer overrun in secureZeroMemory when using D_InlineAsm_X86_64 or D_InlineAsm_X86
  • Loading branch information
shove70 authored Sep 5, 2023
2 parents 4cc4758 + c7de86f commit 42d9b50
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/crypto/utils.d
Original file line number Diff line number Diff line change
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 42d9b50

Please sign in to comment.