Are buffer overflow exploits possible in C#?

Yes, but they are much harder to produce. You can only get buffer overflows if you use certain unsafe constructs, not with “normal” C# code. Memory corrupting code shouldn’t be possible at all, when your code is running with lowered trust.

A few possibilities for buffer overflows:

  1. Using the unsafe keyword, which allows pointers. Unsafe code is just as easy to get wrong, as pointer based code in C or C++.
  2. Using unsafe APIs, such as the methods from the Marshal class
  3. (Mono only) You can disable array range checking (safety vs. performance trade-off)

There are also a few other ways to corrupt memory apart from buffer overflows.

  1. StructLayoutKind.Explicit
  2. Wrong native interop signatures

(The runtime itself is written in C++, so a bug in the runtime can also corrupt memory or overflow a buffer, but I consider that out of scope for this question)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)