I'm interested to know, is Delphi vulnerable to Buffer overflow attack? I read some pages which mentioned Delphi is secure to that vuln because "Delphi can use Pascal strings as well as generic windows strings (PChar). When interfacing with Win API there is no other option except using Pchar". is that true? thanks
-
2You should find a better site to use for learning Delphi. And no, we cannot suggest one for you, because that would be against this site's guidelines.Ken White– Ken White2021-01-08 00:23:24 +00:00Commented Jan 8, 2021 at 0:23
-
Delphi compiler can generate "special" code that can detect some cases of buffer overflow. Read about Range Checking and also Overflow Checking.Error - CPU Not Foud– Error - CPU Not Foud2024-04-11 06:19:39 +00:00Commented Apr 11, 2024 at 6:19
-
Also see this extended discussion: en.delphipraxis.net/topic/…Error - CPU Not Foud– Error - CPU Not Foud2024-04-16 06:57:22 +00:00Commented Apr 16, 2024 at 6:57
3 Answers
is Delphi vulnerable to Buffer overflow attack?
MOST languages are susceptible to buffer overflow attacks. A buffer overflow is a coding bug, not a language defect. For example, in Delphi:
var
buf: array[0..0] of Byte;
i: Integer;
begin
Move(buf, i, sizeof(i)); // buffer overflow!
PInteger(@buf)^ := i; // buffer overflow!
end;
MOST languages will let you shoot yourself in the foot, if you are not careful. There is only so much hand-holding a compiler can do. Not everything can be avoided at compile-time. Programming is not just about writing code that compiles, but also about writing code that acts correctly and responsibly at runtime.
SOME languages may wrap buffers in such a way that bounds checking is performed at runtime, mitigating the risk of buffer overflows. Delphi is not one of those languages, since it allows you to operate directly on raw memory, so you can pretty much do whatever you want (well, whatever the underlying OS lets you do, anyway). And this is certainly true for Pascal strings.
I read some pages which mentioned Delphi is secure to that vuln because "Delphi can use Pascal strings as well as generic windows strings (PChar).
Delphi has no features to avoid all possible kinds of buffer overflows. But, if you write your code to use buffers correctly and sanely, overflows are not likely to happen. This is not limited to just strings, either.
When interfacing with Win API there is no other option except using Pchar". is that true?
It depends on the particular API. Most use simple null-terminated PChar strings, yes. But some use UNICODE_STRING records instead, which use WideChar buffers that are not guaranteed to be null-terminated. Some use ActiveX/COM BSTR (Delphi WideString) strings instead.
4 Comments
Delphi as IDE? Maybe. As language? Sure. Judge for yourself:
var
s: String;
i: Integer;
begin
s:= 'four'; // Length of string: 4 characters
for i:= 1 to 1138 do begin // This loop goes WAY beyond the String's buffer
write( s[i] ); // What will it access after i=4?
end;
end;
PChar is needed because the WinAPI is not constructed for Pascal, so Pascal needs to bend for APIs. Buffer overflows are a problem, but it's not like PChar is radioactive and String is Jesus - it's up to the programmer to not be overly stupid.
6 Comments
i and s is okay for you? That's not the point in this example - the arbitrary loop condition was on purpose and you know that. What you don't seem to know: you can edit comments so you don't have to mistake this as chat to submit multiple comments; likewise you can delete existing ones and add a new/better/combined one. Also consider using formattings in comments.Buffer overflow attack is not related to any specific language. Those attacks are only possible when the developer wrongly coded his application.
To make it short, it is your responsibility as the developer to write all tests when memory is being written with data from the outside. You have to ALWAYS check if the data length is correct to fit when you write it.
For Delphi, there are tools that helps detect buffer overflow (or underflow and many other bugs). For example madExcept. This tool won't prevent buffer overflow, it will immediately if your program overflow a dynamically allocated buffer. This is a test tool that should not be delivered in released version.