You're describing a buffer overflow, and yes, they are a large source of security problems in software. If someone can overrwrite program code with arbitrary data, and that arbitrary data contains executable code of the attacker's choice, then they can essentially execute machine code with the priviledge level of the program in which the overflow occurred.
This problem usually happens when a fixed amount of storage is allocated for an unknown amount of input (from keyboard, network, API call, etc.), and the amount of input turns out to be larger than the size of the storage. In programming languages that do not perform bounds checking on array accesses, this can result in executable areas of code being overwritten. Technologies like DEP can mitigate this risk by write-protecting executable areas of memory.