A good book for this sort of thing is actually one that doesn't mention "exploit" in it at all: Gödel, Escher, Bach: An Eternal Golden Braid by Douglas Hofstadter. The book is a rambling and entertaining treatise on artificial and real intelligence, mathematics, and formal systems. It relates to computer security and exploitation as it describes various formal systems, and how each is limited to its own consistency.
What do SQL injection, buffer overflows, XSS, etc. all have in common? They are results of a system of symbols (SQL, call stack, HTML) that cannot guarantee its own correctness. This is similar to the halting problem. You can't allow arbitrary inputs of the symbols of a system into that system and expect to guarantee anything about the outcome of that system. If you let user input to a SQL statement include single-quote characters (which have special significance), then the outcome of executing the user input can be catastrophically different than the programmer intended.
You asked for theory, and this is about as theoretical and foundational as it gets. Thanks for reminding me to reread GEB:EGB, because it's one of those things you just don't completely absorb the first time through.
EDIT: A couple more foundational documents that really help to understand the scope and history of this branch of information science: