liveBook · Manning

Memory safety

As explained in Hacking Cryptography

Memory safety is the property that program operations access memory only within valid bounds and lifetimes without invalid aliasing or use-after-release.

Memory safety keeps operations within valid memory bounds and prevents user-controlled data from overwriting unrelated code or data. Because code and data share memory, an unchecked input can overflow its buffer and potentially redirect execution. section 10.6

Heartbleed-style overread

A heartbeat request declares a long payload but supplies little data. Vulnerable code trusts that length and calls memcpy(bp, pl, payload), reading beyond the heartbeat buffer into other memory and returning its contents. section 10.6

Apply memory-safety defenses operationally:

  • Avoid C and C++ functions without length checks, including strcpy(), sprintf(), gets(), and scanf(). section 10.6
  • Use strncpy(), snprintf(), fgets(), and sscanf(), and verify explicit buffer bounds when using memcpy(). section 10.6
  • Combine careful parsing with extensive testing and fuzzing, while avoiding integer overflows, type-safety problems, and undefined behavior. section 10.9
Figure 10.9 The Heartbleed vulnerability: a malicious client requests a very long heartbeat but provides very little data to echo. The vulnerable server reads beyond the heartbeat buffer into other areas of memory.
figure

Use the Heartbleed diagram to trace how a declared payload length makes a server read beyond its buffer. section 10.6

How Hacking Cryptography covers it

How deep it goes
Explains buffer-overflow and overread mechanics, names mitigations, and stops short of full memory-management internals.
Examples
Uses server input overflow, bounded C functions, and the OpenSSL 1.0.1f Heartbleed implementation as concrete examples.
What you can do after
Diagnose buffer-boundary failures and choose bounded APIs, testing, fuzzing, and platform mitigations.
How it connects
Connects memory safety with buffer overflows, address space layout randomization, data execution prevention, stack canaries, and type safety.

Also explained in

Compare how 3 books explain memory safety →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.