120 likes | 264 Views
Secure Programming. CSIS 5857: Encoding and Encryption. Secure Programming. Writing code resistant to attacks Buffer overflows Access control SQL injection … Main goal for encryption: Make sure secrets kept Keys Plaintext, etc. Keeping Secrets.
E N D
Secure Programming CSIS 5857: Encoding and Encryption
Secure Programming • Writing code resistant to attacks • Buffer overflows • Access control • SQL injection • … • Main goal for encryption: Make sure secrets kept • Keys • Plaintext, etc.
Keeping Secrets • Cipher keys must exist in memory at some point • Even if encrypted for storage, must be unencrypted to perform encryption/decryption Computer memory Code for Encryption program P C Read in from long term storage (should be encrypted)
Keeping Secrets • Must make sure that memory erased when key no longer needed! • Otherwise, potentially vulnerable to anyone who can later access that memory • Stolen laptop • Access to multiuser system (such a server) • … Computer memory I have you now!
Dynamic Memory • Local memory deallocated when function completed • Must make sure same true for dynamically allocated memory on heap • Objects allocated with new in C, C++, Java… Computer memory Code for Encryption program Object pointer deallocate
Deallocation in C/C++ • Easiest solution: manual deallocation • memset in C • delete in C++ • Problem: C/C++ has other flaws • Easy to create buffer overflow attacks with dynamic pointers/lack of array bounds checking KeyObject k = new KeyObject(decrypt(key));… delete k;
Deallocation in Java • No manual deallocation in Java • Unused memory garbage collected instead • Java memory regularly scanned for memory not used by any active reference • Can manually invoke garbage collection • System.gc(); Method constructs dynamic objects containing key plaintext = decrypt(ciphertext); System.gc(); Clean up those objects when done
Deallocation and Optimization • Problem: Compilers often optimize away this kind of code! • Turn off option in compiler • Test to make sure secrets actually destroyed (print contents of memory location) KeyObject k = new KeyObject(decrypt(key));… delete k; Compiler “This code doesn’t do anything useful, so I’ll skip it”
Swap Files in Virtual Memory • Most OS use virtual memory to run programs • Some program data kept in swap files outside local memory for fast access if local memory full • Even if local memory destroyed, may still exist as copy in swap file! swap file local memory Code for Encryption program deallocate
Swap Files in Virtual Memory • Some OS allow “locking” to prevent specific data from being copied • Some OS encrypt swapped data • May not be default – must specify swap file local memory Code for Encryption program deallocate
Data Retention by Hardware • Data stored in same location in memory for long time can persist even after computer shut down • Keys often stored in same location for entire session • Potentially vulnerable to cold boot attack • Data stored for long time in SRAM becomes preferred startup state • Data stored for long time in DRAM can create magnetic patterns
Data Retention by Hardware • “Boojum” Solution (Furguson, Schneier, Kohno) • Don’t store key directly • Store as components in different locations k1 = R (random number) k2 = h(R) k (hash of random number key) • To recover key: k = h(k1) k2 • Generate new Rfrequently (every second) to avoid long-term patterns in memory k