aboutsummaryrefslogtreecommitdiff
path: root/slides/2018-software-security/src/body.tex
blob: 0eb89e1a3bb510f6d947f9502b21f2986dcc5b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
\begin{frame}
\frametitle{Learning outcomes}
  \vfill\centering
  \smartdiagramset{
    sequence item height=1.75cm,
    sequence item width=2.5cm,
    sequence item text width = 2.4cm,
  }
  \smartdiagram[sequence diagram]{%
    What is software security,%
    Software security gone wrong,%
    Buffer overflows and
    defenses%
  }
  \pause
  \vfill\centering\textbf{Already covered here or elsewhere:} least privilege,
    modern crypto, use secure APIs, pass strings to complex subsystems with
    care, do unit testing, security audits, ...
\end{frame}

\begin{frame}
  \frametitle{Be aware of other common coding mistakes not covered here}
  \centering\includegraphics[width=\textwidth]{img/cwe}
  \burl{https://cwe.mitre.org/data/definitions/699.html}
\end{frame}

\begin{frame}
  \frametitle{Software vulnerabilities pay\titlefloatright{Zerodium's program}}
  \centering\includegraphics[height=0.8\textheight]{img/zerodium}\\
  \burl{https://zerodium.com/program.html}
\end{frame}

\begin{frame}
  \frametitle{Software vulnerabilities pay\titlefloatright{Google's 
    program}}
  \centering\includegraphics[height=0.8\textheight]{img/google-rewards}\\
  \burl{https://www.google.com/about/appsecurity/reward-program/index.html}
\end{frame}


\begin{frame}
  \frametitle{%
    What is software security about?%
    \titlefloatright{%
      Weird machines\footnote{\burl{https://en.wikipedia.org/wiki/Weird_machine}}%
    }%
  }
  \centering
  \includegraphics[height=0.4\textheight]{img/weird-machine}
  \\\vfill
  Security properties and threat model $\rightarrow$
  program should work as intended
\end{frame}

\begin{frame}
  \frametitle{Can you spell software vulnerability? \titlefloatright{BUG}}
  \centering\includegraphics[height=0.5\textheight]{img/real-bug}
  % REMEMBER@bug: bad composition, implementation error, logical error
\end{frame}

\begin{frame}
  %REMEMBER@bad comp: Mat Honan
  \frametitle{Bad composition \titlefloatright{Gmail password
    reset\footnote{%
      \burl{https://www.wired.com/2012/08/apple-amazon-mat-honan-hacking/}
      \titlefloatright{Credit: Nickolai Zeldovich and James Mickens}
    }}}
  \centering
  \begin{tikzpicture}
    \node[draw=gray, rounded rectangle, fill=gray!25, minimum width=4cm,
      minimum height=2cm](gmail) at (0,0) {%
        \begin{tabular}{c}
          \textbf{Gmail reset}\\
          \begin{tabular}{l}
            \textbf{-} use backup mail\\
            \textbf{-} \texttt{m****n@me.com}\\
            \\
          \end{tabular}
        \end{tabular}
      };
    \pause

    \node[draw=gray, rounded rectangle, fill=gray!25, minimum width=4cm,
      minimum height=2cm, below=1cm of gmail](appleid) {%
        \begin{tabular}{c}
          \textbf{AppleID reset}\\
          \begin{tabular}{l}
            \textbf{-} mail address\\
            \textbf{-} billing address\\
            \textbf{-} last four in CC\\
          \end{tabular}
        \end{tabular}
      };
    \pause

    \node[draw=gray, rounded rectangle, fill=gray!25, minimum width=4cm,
      minimum height=2cm, right=3cm of gmail](amazon) {%
        \begin{tabular}{c}
          \textbf{Amazon reset}\\
          \begin{tabular}{l}
            \textbf{-} name \\
            \textbf{-} billing address\\
            \textbf{-} CC number\\
          \end{tabular}
        \end{tabular}
      };
    \pause

    \node[draw=gray, rounded rectangle, fill=gray!25, minimum width=4cm,
      minimum height=2cm, below=1cm of amazon](amazonadd) {%
        \begin{tabular}{c}
          \textbf{Amazon add CC}\\
          \begin{tabular}{l}
            \textbf{-} name \\
            \textbf{-} mail address\\
            \textbf{-} billing address\\
          \end{tabular}
        \end{tabular}
      };
      
    \pause
    \path[draw,thick,-latex]
      (amazonadd) --
        node[draw=none,right]{precaution\footnotemark}
      (amazon);
    \pause
    \path[draw,thick,-latex] (amazon) -- (appleid);
    \pause
    \path[draw,thick,-latex] (appleid) -- (gmail);
  \end{tikzpicture}
  \footnotetext{\tiny{Obfuscate CC, s.t., only last last 4 CC digits are shown}}
\end{frame}

\begin{frame}
  \frametitle{Implementation error\titlefloatright{%
    iCloud hack\footnote{%
      \burl{https://github.com/hackappcom/ibrute}
      \titlefloatright{Credit: Nickolai Zeldovich and James Mickens}
  }}}
  \centering
  \begin{tikzpicture}
    \node[draw=gray, thick, rounded rectangle, minimum width=4cm, minimum 
      height=2cm, fill=gray!25] (icloud) at (0,0) {iCloud};
    \node[draw=none, left=2cm of icloud](ifile) {Files};
    \node[draw=none, above=1cm of icloud](ifeature) {Other features};
    \coordinate(imid) at ($ (ifile.north) !.5! (ifeature) $);
    \node[draw=none] (isharing) at ($ (imid) + (-.75,.5) $) {Sharing};
    \node[draw=none, right=2cm of icloud](ifind) {%
      \begin{tabular}{c}
        Find my\\
        iPhone
      \end{tabular}
    };
    \path[draw, -latex] (ifile) -- (icloud);
    \path[draw, -latex] (isharing) -- (icloud);
    \path[draw, -latex] (ifeature) -- (icloud);
    \path[draw, -latex] (ifind) -- (icloud);
  \end{tikzpicture}
  \pause
  \vfill
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \vbox to .2\textheight{
        \vfill
        \begin{itemize}
          \item User must login to use a feature
          \pause
          \item \textbf{Rate limited login attempts?}
        \end{itemize}
        \vfill
      }
    \end{column}
    \begin{column}{0.49\textwidth}
      \vbox to .2\textheight{
        \vfill
        Files, sharing, other features?
          \titlefloatright{\textcolor{darkGreen}{Yep}}\\
        \pause
        Find my iPhone...?
          \titlefloatright{\textcolor{darkRed}{Nop}}
        \vfill
      }
    \end{column}
  \end{columns}
  \pause
  \vfill
  \alert{Lesson learned: the importance of testing against abnormal behaviour}
\end{frame}

\begin{frame}
  \frametitle{Implementation error\titlefloatright{Subject
    names and TLS\footnote{%
      \burl{https://www.blackhat.com/presentations/bh-usa-09/MARLINSPIKE/BHUSA09-Marlinspike-DefeatSSL-PAPER1.pdf}
    }}}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \begin{itemize}
        \item TLS certificate: identity-to-key binding
        \pause
        \item Subject name? Pascal string
          \begin{itemize}
            \item Length followed by characters
          \end{itemize}
        \pause
        \item Many TLS implementations? C string
          \begin{itemize}
            \item Characters with null-termination
          \end{itemize}
      \end{itemize}
    \end{column}
    \pause
    \begin{column}{0.39\textwidth}
      \includegraphics[width=\textwidth]{img/bad-encode}
    \end{column}
  \end{columns}
  \pause
  \vfill\centering\alert{Lesson learned: only process data at uniform formats}
\end{frame}

\begin{frame}
  \frametitle{The bad news :/}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \begin{itemize}
        \item Much software is written in \texttt{C}/\texttt{C++}
        \item Recipe for disaster:
          \begin{itemize}
            \item Exposure to raw memory addresses
            \item No built-in bound checking and safety
            \item Operate on untrusted user input
          \end{itemize}
        \item Why?
      \end{itemize}
    \end{column}
    \begin{column}{0.39\textwidth}
      \centering\includegraphics[height=0.6\textheight]{img/clang}\\
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{It is easy to get wrong \titlefloatright{1/2}}
  \vfill
  \begin{columns}
    \begin{column}{0.33\textwidth}
      \vbox to .4\textheight{
        \centering
        \lstinputlisting[style=CStyle]{sample/bad-index.c}
        \vfill
        Problem? \\
        \pause
        \textbf{over-read} \\
        \pause
      }
    \end{column}
    \begin{column}{0.33\textwidth}
      \vbox to .4\textheight{
        \centering
        \lstinputlisting[style=CStyle]{sample/bad-index2.c}
        \vfill
        Problem? \\
        \pause
        \textbf{over-write} \\
        \pause
      }
    \end{column}
    \begin{column}{0.33\textwidth}
      \vbox to .4\textheight{
        \centering
        \lstinputlisting[style=CStyle]{sample/bad-printf.c}
        \vfill
        Problem? \\
        \pause
        \textbf{over-read} \\
      }
    \end{column}
  \end{columns}
  \vfill
\end{frame}

\begin{frame}
  \frametitle{It is easy to get wrong \titlefloatright{2/2}}
  \vfill
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \vbox to .6\textheight{
        \centering
        \lstinputlisting[style=CStyle]{sample/bad-call.c}
        \vfill
        Problem? \\
        \pause
        \textbf{over-write}
        \pause
      }
    \end{column}
    \begin{column}{0.49\textwidth}
      \vbox to .6\textheight{
        \centering
        \lstinputlisting[style=CStyle]{sample/bad-add.c}
        \vfill
        Problem? \\
        \pause
        \textbf{over-write}
      }
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{A famous over-read in OpenSSL\titlefloatright{%
    Heartbleed bug%
    \includegraphics[height=16pt]{img/heartbleed}%
  }}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \includegraphics[width=\textwidth]{img/hb1}
      \includegraphics[width=\textwidth]{img/hb2}
      \includegraphics[width=\textwidth]{img/hb3}
    \end{column}
    \begin{column}{0.49\textwidth}
      \includegraphics[width=\textwidth]{img/hb4}
      \includegraphics[width=\textwidth]{img/hb5}
      \includegraphics[width=\textwidth]{img/hb6}
    \end{column}
  \end{columns}
  \vfill\centering\burl{https://xkcd.com/1354/}
  %REMEMBER@after heartbleed: transition -> easy bug, not the typical case.  Usually, attckers exploit buffer overflows (i.e., write outside of buffer boundaries) to change the program flow, e.g., by changing the value of adjacent variables. In extreme cases, entirely new functionality can be inserted - hello world program with buf overflow -> delete files...  This is what we will work towards now.
\end{frame}

\begin{frame}
  %REMEMBER: explain stack briefly, and paint everything on board - local vars
  \frametitle{Buffer overflow \titlefloatright{Stack}}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \lstinputlisting[style=CStyle]{sample/bo-stack.c}
    \end{column}
    \pause
    \begin{column}{0.39\textwidth}
      \begin{itemize}
        \item Input 1: Neal \ding{224} \textcolor{darkRed}{reject}
        \pause
        \item Input 2: Caffrey \ding{224} \textcolor{darkRed}{reject}
        \pause
        \item Input 3: Overflow! \ding{224} \textcolor{darkGreen}{accept}
        \pause
      \end{itemize}
    \end{column}
  \end{columns}
  \vfill\centering\alert{Buffer overflow occurs when writing outside of a buffer's boundaries}
\end{frame}

\begin{frame}
  %REMEMBER: explain heapy briefly, and paint everything on board - need var lifetime that is longer than function lifetime
  \frametitle{Buffer overflow \titlefloatright{Heap}}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \lstinputlisting[style=CStyle]{sample/bo-heap.c}
    \end{column}
    \pause
    \begin{column}{0.39\textwidth}
      \begin{itemize}
        \item Input 1: Neal \ding{224} \textcolor{darkRed}{reject}
        \pause
        \item Input 2: Caffrey \ding{224} \textcolor{darkRed}{reject}
        \pause
        \item Input 3: aaa...aaa \ding{224} \textcolor{darkGreen}{accept}
      \end{itemize}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  % Just to make sure that everyone is on the same page now, and work towards stack frames
  \frametitle{Memory layout}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \begin{itemize}
        \item Heap grows towards higher addresses
          \begin{itemize}
            \item Manual memory (de)allocation
          \end{itemize}
        \item Stack grows towards lower addresses
          \begin{itemize}
            \item Automatic memory (de)allocation
            \item Each function has a `stack frame'
          \end{itemize}
        \item Data: e.g., global and static variables
        \item Code: instructions that CPU can process
      \end{itemize}
    \end{column}
    \begin{column}{0.39\textwidth}
      \centering\includegraphics[height=0.6\textheight]{img/memlay}
    \end{column}
  \end{columns}
  %\pause
  %\vfill\centering\alert{Note: each process has its own virtual memory that
  %  is translated to physical memory}
\end{frame}

\begin{frame}
  \frametitle{Stack frames}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \lstinputlisting[style=CStyle]{sample/stack-frame.c}
    \end{column}
    \begin{column}{0.49\textwidth}
        Each function gets its own stack frame
        \begin{itemize}
          \item Local variables
          \item Function parameters
          \item Housekeeping such as:
            \begin{itemize}
              \item Return address
              \item Register values
            \end{itemize}
        \end{itemize}
        \vspace{12pt}
        \alert{Push ordering} \ding{224}
        \alert{see calling conventions}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Pushing an popping stack frames}
  \begin{columns}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
      \end{drawstack}
    \end{column}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
        \cell{sub}
      \end{drawstack}
    \end{column}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
        \cell{sub}
        \cell{add}
      \end{drawstack}
    \end{column}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
        \cell{sub}
      \end{drawstack}
    \end{column}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
      \end{drawstack}
    \end{column}
    \begin{column}{0.166\textwidth}
      \begin{drawstack}[scale=0.5]
        \cell{algorithm}
        \cell{printf}
      \end{drawstack}
    \end{column}
  \end{columns}
  \vfill\centering\alert{Return to caller's context using housekeeping
    information}
\end{frame}

\begin{frame}
  \frametitle{Buffer overflow on the stack continued \titlefloatright{(1/2)}}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \lstinputlisting[style=CStyle]{sample/bo-stack.c}
    \end{column}
    \begin{column}{0.39\textwidth}
      \begin{itemize}
        \item Input 4: aaa...aaa \ding{224} \textcolor{darkRed}{segfault}
        \item Why not segfault on heap?
        \item Why segfault on stack?
      \end{itemize}
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}
  \frametitle{Buffer overflow on the stack continued \titlefloatright{(2/2)}}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      %\lstinputlisting[style=CStyle,firstline=3,lastline=4,numbers=none]{%
      %  sample/bo-stack.c
      %}
      \lstinputlisting[style=CStyle]{sample/bo-stack.c}
    \end{column}
    \begin{column}{0.49\textwidth}
      \centering
      \begin{drawstack}[scale=0.5]
        \cell{RA} \cellcomL{\scriptsize{0x...\textcolor{darkRed}{32}}}
        \cell{\texttt{verified}}
          \cellcomL{\scriptsize{0x...\textcolor{darkRed}{28}}}
        \padding{3}{%
          \begin{tabular}{c}
            \texttt{buf[7]} \\
            ... \\
            \texttt{buf[0]} \\
            \end{tabular}
        } \cellcomL{\scriptsize{0x...\textcolor{darkRed}{20}}}
      \end{drawstack}
    \end{column}
  \end{columns}
  \pause
  \centering\vfill\alert{%
    Can we solve the problem by pushing \texttt{buf} first?%
  }
\end{frame}

\begin{frame}
  \frametitle{Buffer overflow that leads to code execution}
  \centering\includegraphics[height=0.6\textheight]{img/demo}
\end{frame}

\begin{frame}
  \frametitle{Summary of principles for stack smashing attacks}
  \centering\includegraphics[height=0.6\textheight]{img/stack-smashing}\\
  \vfill
  \alert{1.} Gain control of return address
  \titlefloatright{\alert{2.} Point to some malicious code}
\end{frame}

\begin{frame}
  \frametitle{The hard part is jumping to malicious code \titlefloatright{`In the buffer'}}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \begin{enumerate}
        \item Add asm instructions in the buffer
          \begin{itemize}
            \item Usually to open a shell
            \item `Shell-code'
          \end{itemize}
        \item Jump to the buffer's shell-code
      \end{enumerate}
      \vspace{1cm}\centering
      Exact address of buffer?\\
      \rding\\
      Use a nop-sled
    \end{column}
    \begin{column}{0.49\textwidth}
      \centering
      \begin{tikzpicture}[scale=0.5]
        \stacktop{}
          % cells
          \cell{RA} \coordinate (ra) at (currentcell.east);
          \cell{shell code} \coordinate (sc) at (currentcell.west);
          \bcell{\texttt{nop}} \coordinate(nopl) at (currentcell.west);
          \bcell{...} \coordinate(nopm) at (currentcell.east);
          \bcell{\texttt{nop}} \coordinate(nopf) at (currentcell.west);
            \cellptr{\texttt{\&buf[0]}}
          % air plane
          \coordinate(mid) at ($ (nopl) !.5! (nopf) $);
          \node(plane) at ($ (mid) + (-2,0) $){%
            \includegraphics[width=45pt]{img/air-plane}
          };
          % edges
          \path[draw=darkRed,-latex,rounded corners,dashed,thick]
            (ra) -- ($ (ra) + (2,0) $) |- (nopm);
        \stackbottom{}
      \end{tikzpicture}
    \end{column}
  \end{columns}
  \pause
  \centering\vfill\alert{%
    Note: nop-sled + asm may also be injected to the heap---`heap spraying'
  }
\end{frame}

\begin{frame}
  \frametitle{The hard part is jumping to malicious code \titlefloatright{%
    `Return-to-libc'}}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \begin{enumerate}
        \item Point to an already loaded function
          \begin{itemize}
            \item \texttt{system()}
          \end{itemize}
        \item Prepare stack with arguments
          \begin{itemize}
            \item \texttt{"rm -rf /home/\$USER"}
          \end{itemize}
      \end{enumerate}
      \vspace{12pt}
      \ding{224}\texttt{system("rm -rf /home/\$USER")}
    \end{column}
    \pause
    \begin{column}{0.49\textwidth}
      \centering\includegraphics[height=0.45\textheight]{img/libc}
    \end{column}
  \end{columns}
  \vfill
  \lstinputlisting[style=CStyle,numbers=none]{sample/ldd}
\end{frame}

\begin{frame}
  \frametitle{Widely used stack smashing mitigation techniques}
  \textbf{Idea:} try to prevent the two necessary stack smashing conditions from
    meeting met
  \vfill
  \pause
  \begin{itemize}
    \item<2-> Address randomization \titlefloatright{Increases jump uncertainty}
    \item<3-> Non-executable memory \titlefloatright{Stop if instruction pointer
      gets here}
    \item<4-> Stack canaries \titlefloatright{Stop if RA got tampered with}
  \end{itemize}
  \vfill
  \lstinputlisting[style=CStyle,numbers=none]{sample/ldd2}
\end{frame}

\begin{frame}
  \frametitle{Canaries}
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \centering
      \vbox to .8\textheight{
        \vfill
        \begin{quote}
          `` [...] miners would bring a caged canary into new coal seams. Canaries
          are especially sensitive to methane and carbon monoxide [...], as long
          as the bird kept singing, the miners knew their air supply was safe.''
        \end{quote}
        \vfill
        \begin{quote}
          ``\textbf{Short but meaningful}''
        \end{quote}
        \vfill
      }
    \end{column}
    \begin{column}{0.49\textwidth}
      \centering
      \includegraphics[height=0.8\textheight]{img/canary-bird}\\
      \centering\burl{https://www.wisegeek.com/what-does-it-mean-to-be-a-canary-in-a-coal-mine.htm}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Stack canaries \titlefloatright{Before overflow}}
  \centering
  \includegraphics[width=0.95\textwidth]{img/canary-before}
  \vfill
  \begin{columns}
    \begin{column}{0.49\textwidth}
      \begin{itemize}
        \item Terminator canaries
        \item Random canaries
        \item ...
      \end{itemize}
    \end{column}
    \begin{column}{0.49\textwidth}
      \centering
      \textbf{Require:}\\
      CY must be valid to use RA
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Stack canaries \titlefloatright{After overflow}}
  \centering
  \includegraphics[width=0.8\textwidth]{img/canary-after}
  \vfill
  \textbf{Stop running---invalid canary value!}
  \pause
  \vfill
  \vfill\centering\alert{Can anyone think of examples where the two canary
    types fail?}
  % == stack ==
  % - local variable which is a function pointer gets overwritten; when
  % called the attacker's function is used instead
  % - somehow extract random canary value from memory or rewrite it
  % - guess canary, e.g., due to bad randomness
  % == terminator ==
  % - you have a double buffer overflow vulnerability: first you overwrite as
  % normal to hit RA, then subsequently you `repair' the predictable canary
\end{frame}

\begin{frame}
  \frametitle{Great mitigation techniques, but we realize that...}
  \centering\Huge{%
    it is still imperfect
    \vfill
    What else can we do?
  }
  \pause
  \vfill
  \normalsize
  \begin{columns}
    \begin{column}{0.33\textwidth}
      \begin{itemize}
        \item Avoid bugs in C/C++ code
      \end{itemize}
    \end{column}
    \begin{column}{0.33\textwidth}
      \begin{itemize}
        \item Build and use tools that help catching bugs
      \end{itemize}
    \end{column}
    \begin{column}{0.33\textwidth}
      \begin{itemize}
        \item Use memory safe programming languages
      \end{itemize}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Avoid bugs in C/C\texttt{++} code}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \begin{itemize}
        \item Sanitize all untrusted user input
        \item Manually verify all bounds \alert{correctly}
        \item Be aware of integer underflow/overflow
        \item Use safe(r) functions and learn caveats
          \begin{itemize}
            \item \texttt{fgets} vs.\ \texttt{gets}
            \item \texttt{strncpy} vs.\ \texttt{strcpy}
            \item \texttt{man strncpy} \ding{224} null-termination?
          \end{itemize}
        \item \textbf{Adopt a secure coding standard}
          \begin{itemize}
            \item CERT C\footnotemark
            \item MISRA C\footnotemark  (embedded systems)
          \end{itemize}
      \end{itemize}
    \end{column}
    \begin{column}{0.39\textwidth}
      \centering\includegraphics[height=0.6\textheight]{img/checklist}
    \end{column}
  \end{columns}
  \footnotetext[6]{\burl{https://resources.sei.cmu.edu/downloads/secure-coding/assets/sei-cert-c-coding-standard-2016-v01.pdf}}
  \footnotetext{\burl{https://www.misra.org.uk/Activities/MISRAC/tabid/160/Default.aspx}}
\end{frame}


\begin{frame}
  \frametitle{Build and use tools that help catching bugs \titlefloatright{%
    Static analysis%
    \footnote{\burl{https://www.perforce.com/blog/qac/what-static-code-analysis}}
  }}
  \begin{columns}
    \begin{column}{0.69\textwidth}
      \begin{itemize}
        \item Automated source code analysis \alert{before} runtime
        \item Output warnings if errors are suspected
        \item Assess compliance with coding standards
      \end{itemize}
    \end{column}
    \begin{column}{0.39\textwidth}
      \vbox to .7\textheight{%
        \centering
        \lstinputlisting[style=CStyle]{sample/static-analysis.c}
        \texttt{\$ splint main.c} \\
        \ding{224} 3 non-gcc warnings
        \vfill
        \tiny{\alert{Details:}} \burl{https://www.splint.org/}
      }
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Build and use tools that help catching bugs \titlefloatright{Dynamic
    analysis}}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \begin{itemize}
        \item Analyze what program does at \alert{runtime}
        \item Valgrind---look for memory errors
        \item Fuzzing\footnotemark---what happens on funky input?
          \begin{itemize}
            \item<2-> Random and mutation-based
            \item<3-> Structure-aware
            \item<4-> Program-aware
            \item<5-> Automated feedback loops
            \item<6-> ...
          \end{itemize}
      \end{itemize}
    \end{column}
    \begin{column}{0.39\textwidth}
      \vbox to .8\textheight{
        \vfill\centering
        \includegraphics[width=\textwidth]{img/afl.jpg}
        \burl{https://upload.wikimedia.org/wikipedia/commons/0/08/Rabbit_american_fuzzy_lop_buck_white.jpg}
        \vfill
      }
    \end{column}
  \end{columns}
  \footnotetext{\tiny{Brief introduction and a few demos:}
    \burl{https://www.youtube.com/watch?v=dMmsPwkSqOc}}
\end{frame}

\begin{frame}
  \frametitle{Is fuzzing worth the effort? \titlefloatright{American Fuzzer Lop
    (AFL)}}
  \centering
  \includegraphics[width=0.8\textwidth]{img/afl}\\
  \burl{http://lcamtuf.coredump.cx/afl/}
  \vfill\normalsize\alert{And around 120 more projects!}
\end{frame}

\begin{frame}
  \frametitle{Use memory-safe programming languages}
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \vbox to 0.5\textheight{%
        \textbf{Intuition:} \texttt{x[y] = z} should stop normal program
          execution if \texttt{x} is non-array or \texttt{y} is out-of-range,
          and you should not operate on raw memory addresses\footnotemark
        \vfill
        \only<2->{
        \textbf{Challenges:}
        \begin{itemize}
          \item You need low-level access to hardware
          \item You inherit a large \texttt{C}/\texttt{C++} project
          \item Someone must implement the core correctly
        \end{itemize}
        }
      }
    \end{column}
    \begin{column}{0.39\textwidth}
      \begin{tikzpicture}
        \node[draw=none](first) at (0,0) {%
          \includegraphics[height=2cm]{img/memory-safe/go}
          \includegraphics[height=2cm]{img/memory-safe/rust}
        };
        \node[draw=none, below =0pt of first] (second) {%
          \includegraphics[height=1cm]{img/memory-safe/python}
        };
        \node[draw=none, below =0pt of second] (third) {%
          \includegraphics[height=1.5cm]{img/memory-safe/java}
        };
        \coordinate(mycord) at ($ (first.east) !.5! (third.east) $);
        \node[draw=none](fourth) at ($ (mycord) !.5! (third) $) {%
          \hspace{2cm}\huge{\texttt{C\#}}
        };
      \end{tikzpicture}
    \end{column}
  \end{columns}
  \vfill
  \centering
  \begin{description}
    \item<3->[\alert{What about performance?}] \alert{Is it a valid concern?}
  \end{description}
  \footnotetext{\tiny{If you want a more precise intuition:}
    \burl{http://www.pl-enthusiast.net/2014/07/21/memory-safety/}
  }
\end{frame}

%\begin{frame}
%  \frametitle{Take a deeper dive into buffer overflow vulnerabilities and
%    defenses}
%  \vfill
%  \begin{columns}
%    \begin{column}{0.33\textwidth}
%      \centering
%      \includegraphics[width=\textwidth]{img/mit1}
%      \burl{https://www.youtube.com/watch?v=GqmQg-cszw4&list=PLUl4u3cNGP62K2DjQLRxDNRi0z2IRWnNh&index=1}
%    \end{column}
%    \begin{column}{0.33\textwidth}
%      \centering
%      \includegraphics[width=\textwidth]{img/mit2}
%      \burl{https://www.youtube.com/watch?v=r4KjHEgg9Wg&index=2&list=PLUl4u3cNGP62K2DjQLRxDNRi0z2IRWnNh}
%    \end{column}
%    \begin{column}{0.33\textwidth}
%      \centering
%      \includegraphics[width=\textwidth]{img/mit3}
%      \burl{https://www.youtube.com/watch?v=xSQxaie_h1o&index=3&list=PLUl4u3cNGP62K2DjQLRxDNRi0z2IRWnNh}
%    \end{column}
%  \end{columns}
%  \vfill\centering\alert{Optional!}
%\end{frame}

\begin{frame}
  \frametitle{Summary}
  \begin{columns}
    \begin{column}{.59\textwidth}
      \begin{itemize}
        \item Weird machines
        \item Over-read, over-write
        \item Change program flow
        \item Mitigation techniques
        \item Tools and other options
      \end{itemize}
    \end{column}
    \begin{column}{.49\textwidth}
      \centering\includegraphics[width=\textwidth]{img/take-away}
    \end{column}
  \end{columns}
  %\centering\includegraphics[height=0.8\textheight]{img/summary}
\end{frame}

\begin{frame}
  \frametitle{Assignment questions \titlefloatright{(1/3)}}
  \alert{1.} Suppose that the code below is compiled as follows:
    \texttt{gcc -Wall -Werror -std=c99 main.c}. Provide two \alert{integer
  inputs} that will result in `unintended behaviour' and name what this
  threat is called. Make assumptions if necessary.
  % Assume int is 4 bytes.
  % - Number 1: 1
  % - Number 2: 2147483647        // 2^31 - 1
  % => 1+2147483647 <= 10
  % (integer overflow)
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \lstinputlisting[style=CStyle]{sample/assignment-integer-overflow.c}
    \end{column}
    \begin{column}{0.39\textwidth}
      \vbox to .65\textheight{
        \vfill
        \alert{2.} Determine which compiler option could be used to ensure
          that the program aborts if such unintended behaviour occurs. Does
          this solution work for \texttt{unsigned int}s? Why (not)? \\
          % -ftrapv: abort if signed integer overflow. Does not work for
          % unsigned since overflow is well-defined and in many cases intended.
        \vfill
        \alert{3.} What is the compliant way of adding two unsigned integers
          according to CERT C standard?
          % §5.1.1.2, page 133 (find link to CERT C pdf in the slides)
          %   unsigned int ui_b, ui_a, sum;
          %   if (UINT_MAX - ui_a < ui_b) {
          %       /* handle error condition */
          %   } else {
          %       sum = ui_a + ui_b;
          %   }
        \vfill
      }
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Assignment questions \titlefloatright{(2/3)}}
  \alert{4.} Suppose that the code below is compiled as follows: \texttt{gcc
    -Wall -Werror -std=c99 -fno-stack-protector main.c}. Explain the steps
    necessary to trigger the print statement. Make assumptions if necessary.
    % Assume that secret is 4 bytes and on a higher address than buf.
    % Exploit: python -c 'print "\xff"*12' | ./a.out
  \begin{columns}
    \begin{column}{0.59\textwidth}
      \lstinputlisting[style=CStyle]{sample/assignment-buffer-overflow.c}
    \end{column}
    \begin{column}{0.39\textwidth}
      \vbox to .65\textheight{
        \vfill
        \alert{5.} How would you adapt your strategy if \texttt{SECRET} was set
          to \texttt{0xff0a0dff}? Explain principles.
          % If we try to enter this value gets() will stop since CRLF. Instead
          % we would have to overwrite the return address so that the program
          % jumps to gotcha().
        \vfill
        \alert{6.} Which type of buffer overflow mitigation technique does the
          new secret value remind you of?
          % Terminator-based stack canary
        \vfill
        \alert{7.} Explain two other mitigation techniques that make it harder
          to execute code in a buffer overflow.
          % - Address randomization: every time program executes all addresses
          % are changed based on a cryptographically secure PRNG
          % - Non-executable memory: halt program if instruction pointer takes
          % on a value that corresponds to memory that should not be executed
          % 
          % In other words: both of these techniques try to make it hard for
          % the attacker to specify a return address that does not cause a crash
      }
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Assignment questions \titlefloatright{(3/3)}}
  \vfill
  \alert{8.} Attackers may use \texttt{nop}-sleds to increase the likelihood of
    jumping to their shell-code. To defend against this a colleague of yours
    suggested that all user input be filtered for repeated \texttt{nop}
    instructions. How would you trivially bypass such a filtering mechanism?
    % Use other dummy instructions that increment instruction pointer, e.g.,
    % add, sub, mul, and write results to dummy register (not important).
  \vfill
  \alert{9.} What is the difference between static and dynamic code analysis?\\
    % Static code analysis takes place _before_ a program is being executed.
    % Conversely, dynamic code analysis examines what a program does at runtime.
  \vfill
  \alert{10.} Briefly explain the process of fuzzing a program: how does it work
    and what is the goal? Name one fuzzer that found a buffer overflow
    vulnerability in a TLS library.
    % - How: supply lots and lots of funky input and see what happens; record
    % errors % and examine in more detail later.
    % - Goal: find untested cases that cause `unintended behaviour'
    % - Example: American Fuzzy Lop (AFL)
  \vfill
  \alert{11.} Suppose that you are hired by a consultant company to work on a
    brand new project. Explain the circumstances in which you would choose to
    program in \texttt{C/C++}, and why you might choose a different programming
    language in most other cases.
    % C/C++: if low-level access to memory is _required_, i.e., there should
    % be a very clear motivation why you must use C as opposed to Go,C#, ...
    % Memory safe languages: would be chosen otherwise, since it is generally
    % easier not to mess up the code.
  \vfill
\end{frame}