SWEN-331: Engineering Secure Software Benjamin S Meyers
Defensive Coding Techniques (Pt. 1)
Engineering Secure Software
Last Revised: September 21, 2020 1
Defensive Coding Techniques (Pt. 1) Engineering Secure Software - - PowerPoint PPT Presentation
Defensive Coding Techniques (Pt. 1) Engineering Secure Software Last Revised: September 21, 2020 SWEN-331: Engineering Secure Software Benjamin S Meyers 1 Defensive Coding vs. Risk Analysis Risk Analysis All about domain, assets,
SWEN-331: Engineering Secure Software Benjamin S Meyers
Last Revised: September 21, 2020 1
SWEN-331: Engineering Secure Software Benjamin S Meyers
2
SWEN-331: Engineering Secure Software Benjamin S Meyers
3
SWEN-331: Engineering Secure Software Benjamin S Meyers
4
SWEN-331: Engineering Secure Software Benjamin S Meyers
5
SWEN-331: Engineering Secure Software Benjamin S Meyers
■ Java: https://www.oracle.com/java/technologies/javase/seccodeguide.html ■ C++: https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=88046682
6
SWEN-331: Engineering Secure Software Benjamin S Meyers
7
SWEN-331: Engineering Secure Software Benjamin S Meyers
8
SWEN-331: Engineering Secure Software Benjamin S Meyers
9
SWEN-331: Engineering Secure Software Benjamin S Meyers
10 10
str = “123 <script>” return “$#{str.to_i}” # returns “$123” i = “123 <script>” return “$” + str(int(re.sub(r“[^0-9]”, “”, i))
SWEN-331: Engineering Secure Software Benjamin S Meyers
11 11
SWEN-331: Engineering Secure Software Benjamin S Meyers
12 12
SWEN-331: Engineering Secure Software Benjamin S Meyers
13 13
public void something() { Connection conn = null; try { conn = getConnection(); // do db stuff } catch (SQLException e) { // handle the exception } finally { DBUtil.closeConnection(conn); } }
SWEN-331: Engineering Secure Software Benjamin S Meyers
14 14
SWEN-331: Engineering Secure Software Benjamin S Meyers
15 15
public class ClassLoader { public ClassLoader() { securityCheck(); init(); } private securityCheck() { … }; private init() { … }; }
SWEN-331: Engineering Secure Software Benjamin S Meyers
16 16
public class MaliciousCL extends ClassLoader { static ClassLoader cl; @Override protected void finalize() { cl = this; } public static void main(String[] args) { try { new MaliciousCL(); } catch (SecurityException e) { … } System.gc(); System.runFinalization(); System.out.println(cl); } }
SWEN-331: Engineering Secure Software Benjamin S Meyers
17 17
SWEN-331: Engineering Secure Software Benjamin S Meyers
18 18
public static final List<String> list = new ArrayList<String>();
SWEN-331: Engineering Secure Software Benjamin S Meyers
19 19
public static final List<String> list = new ArrayList<String>(); public static final List<String> list = new unmodifiableList(asList(“Alice”, “Bob”, “Charlie”));
SWEN-331: Engineering Secure Software Benjamin S Meyers
20 20
SWEN-331: Engineering Secure Software Benjamin S Meyers
21 21
int *a, *b, *c; a = (int *) malloc(sizeof(int)); // a is now 0x12345678 free(a); // byte 0x12345678 is now available to malloc b = (int *) malloc(sizeof(int)); // b is now 0x12345678! *b = 5; free(a); // free 0x12345678 again?!?! b is now freed too! c = (int *) malloc(sizeof(int)); // c is now 0x12345678 *c = 6; printf (“%d”, *b); // prints 6 not 5! Corrupted!
SWEN-331: Engineering Secure Software Benjamin S Meyers
22 22
int *a, *b, *c; a = (int *) malloc(sizeof(int)); // a is now 0x12345678 free(a); // byte 0x12345678 is now available to malloc a = 0; // zero-out pointer after free, just in case b = (int *) malloc(sizeof(int)); // b is now 0x12345678! *b = 5; free(a); // free 0x12345678 again?!?! b is now freed too! c = (int *) malloc(sizeof(int)); // c is now 0x12345678 *c = 6; printf (“%d”, *b); // prints 6 not 5! Corrupted!
SWEN-331: Engineering Secure Software Benjamin S Meyers
23 23
int *a, *b, *c; a = (int *) malloc(sizeof(int)); // a is now 0x12345678 free(a); // byte 0x12345678 is now available to malloc a = 0; // zero-out pointer after free, just in case b = (int *) malloc(sizeof(int)); // b is now 0x12345678! *b = 5; free(a); // free 0x12345678 again?!?! b is now freed too! c = (int *) malloc(sizeof(int)); // c is now 0x12345678 *c = 6; printf (“%d”, *b); // prints 6 not 5! Corrupted!
Don’t double-free!
SWEN-331: Engineering Secure Software Benjamin S Meyers 24 24
Source: https://xkcd.com/2054/