Lecture 9: Security Considerations
Lecture 9 discusses security considerations in systems programming.
It discusses the key role memory safety plays in secure software,
and highlights the undefined behaviours that results from lack of
memory safety, and that can lead to security vulnerabilities. The
benefits of programming in a strongly typed, memory safe, language
are highlighted. Later parts of the lecture discuss approaches to
safely handling and parsing data received from the network, and
the possible benefits of type-driven design for security.
Part 1: Memory Safety
The first part of the lecture discusses the importance of memory safety,
the role the lack of memory safety plays in security vulnerabilities.
With reference to the type system of a language, it describes what is
memory safety and introduces the concept of undefined behaviour in
languages that are not memory safe. This is followed by a description
of various types of undefined behaviour, relating to lack of memory
safety, that can occur in C programs. The lecture concludes by
highlighting some statistics around the prevalence of memory safety
related security vulnerabilities in systems code over the years, and
suggests various mitigations and approaches to help move away from
memory unsafe code.
00:00:00.000
In this lecture I want to discuss
00:00:02.575
some security considerations when writing systems programs.
00:00:06.690
In this part, I’ll start by talking
00:00:08.970
about memory safety, and some of the
00:00:11.249
issues with memory safety, and the lack
00:00:13.529
of memory safety, and how they affect
00:00:15.809
security vulnerabilities.
00:00:16.560
And then, in the later parts,
00:00:18.744
I’ll talk about parsing and some language
00:00:21.292
theoretic approaches to securing parsing. And I’ll
00:00:23.840
talk about the benefits of modern type
00:00:26.388
systems for security.
00:00:30.270
So let's begin by talking about memory
00:00:32.991
safety, and talk a little about what
00:00:35.712
is memory safety, and the types of
00:00:38.433
undefined, memory-unsafe, behaviours you see in C
00:00:41.153
and C++. I’ll talk about the security
00:00:43.874
impact of memory unsafety, and the mitigations
00:00:46.595
that can be applied.
00:00:52.020
So what is memory safety?
00:00:54.840
Well, a memory safe language is a
00:00:57.632
language that assures that the only memory
00:01:00.425
that can be accessed is that which
00:01:03.217
is owned by the program, and that
00:01:06.010
all access to that memory are done
00:01:08.802
in a manner which is consistent with
00:01:11.594
the types of data.
00:01:13.290
So the program can only access memory
00:01:15.929
through its global variables, through its local
00:01:18.568
variables, or through explicit references from them.
00:01:21.207
It can't access memory which isn't in
00:01:23.846
any of its variables, or isn't referenced
00:01:26.485
from any of those variables.
00:01:28.470
It can only access heap memory if
00:01:30.489
it's got an explicit reference to that
00:01:32.509
memory, and the way in which it
00:01:34.528
accesses memory is consistent with the type
00:01:36.548
of the memory. If the value is
00:01:38.567
written into memory as an integer,
00:01:40.298
it can’t be read back as a
00:01:42.318
floating point value, for example,
00:01:43.860
unless there's an explicit conversion in scope.
00:01:47.833
It can't treat the data inconsistently for
00:01:51.805
different types.
00:01:53.040
A memory unsafe language is a language
00:01:56.001
which fails to prevent accesses which break
00:01:58.962
these typing rules. A memory unsafe language
00:02:01.923
is a language which allows access to
00:02:04.883
memory which the program doesn't explicitly own,
00:02:07.844
or which allows memory to be accessed
00:02:10.805
in a type unsafe way.
00:02:13.020
Now there are many examples of safe
00:02:15.954
languages, memory safe languages:
00:02:17.730
Java, and Scala, and Rust, and Go,
00:02:20.407
and Python, and Ruby, and Tcl,
00:02:22.702
and FORTRAN, and COBOL, and Modula-2,
00:02:24.996
and Occam, Erlang, and Ada, and Pascal,
00:02:27.673
and Haskell and a whole bunch more.
00:02:30.450
The ones, which are unsafe, are a
00:02:34.238
much smaller set: assembly language. C,
00:02:37.485
C++, and Objective C.
00:02:39.750
As far as I know, that's it.
00:02:42.990
And these languages don't prevent accesses which
00:02:46.737
break the type rules, such that they
00:02:50.485
have.
00:02:51.120
What they do is, rather, declare such
00:02:53.710
behaviour to be undefined.
00:02:55.290
They say “you shouldn't do this”,
00:02:57.595
they say “it's not legal to do
00:03:00.283
this”, but in these cases the program
00:03:02.972
will continue
00:03:03.840
if the undefined accesses are performed,
00:03:06.008
it's just not clear what the behaviour
00:03:08.537
will be. It doesn't specify what the
00:03:11.066
behaviour will be.
00:03:15.990
So what does this mean in practice?
00:03:19.380
When a language defines certain types of
00:03:22.975
behaviour to be undefined, what can happen?
00:03:26.569
What types of behaviour are undefined?
00:03:30.750
Well, maybe it's a type unsafe allocation.
00:03:35.850
It’s possible to write C code like
00:03:38.255
we see on the slide here,
00:03:40.316
where we allocate memory to hold the
00:03:42.720
size of floating point value, and we
00:03:45.125
reference it as if it was a
00:03:47.529
double precision float.
00:03:48.660
And in this case, what we see
00:03:51.063
is that the call to malloc() does
00:03:53.465
not check if the amount of memory
00:03:55.868
allocated corresponds to the type by which
00:03:58.270
it's being accessed.
00:03:59.400
On a typical 64-bit machine, this is
00:04:03.178
allocating memory for a 32-bit floating point
00:04:06.955
type, and then when it's accessed,
00:04:10.193
it’s accessed as a 64 bit-type,
00:04:13.431
and overflows the space.
00:04:15.690
A memory safe language would require that
00:04:18.688
the size of the allocation match the
00:04:21.687
size of the value which will be
00:04:24.685
stored in it.
00:04:26.070
If you're allocating space for a floating
00:04:28.538
point value, you store floating point values
00:04:31.007
in it. If you try to access
00:04:33.475
to allocate space for a floating point
00:04:35.944
value and access it as a double,
00:04:38.412
it shouldn't compile.
00:04:39.570
So the operation ought to be,
00:04:41.981
allocate enough memory for an object of
00:04:44.794
type T, and return a reference to
00:04:47.606
type T,
00:04:48.510
which can only be assigned to a
00:04:52.397
variable which holds a reference of that
00:04:56.285
type.
00:04:56.940
What we haven't C, though, is a
00:04:59.467
function which allocates a certain amount of
00:05:01.993
memory and a returns an untyped reference.
00:05:04.620
And that's where
00:05:06.150
the lack of safety comes in;
00:05:08.320
there's no typing of the allocations.
00:05:10.590
And C doesn't know what's going to
00:05:12.297
be stored in a malloc()-ed value,
00:05:13.760
and therefore it can't check that it's
00:05:15.467
big enough to hold the values that
00:05:17.175
are being stored there.
00:05:21.630
We could be doing a use-before-allocation.
00:05:26.190
In this case, we see code which
00:05:29.060
reads from a file descriptor, for example
00:05:31.930
reading from a network, and it has
00:05:34.800
a char* pointer for a buffer into
00:05:37.670
which it is reading, and it calls
00:05:40.540
the recv() call to read into that
00:05:43.410
buffer, but it never allocates the memory
00:05:46.280
for the buffer.
00:05:47.610
So it passes a pointer to the
00:05:49.893
buffer to the recv() function, but it's
00:05:52.176
forgotten to allocate memory,
00:05:53.580
or the programmer has assumed that the
00:05:56.297
recv() function will do the allocation for
00:05:59.014
them. And the result is that it
00:06:01.730
writes to an arbitrary location, depending on
00:06:04.447
what value happens to be in this
00:06:07.164
uninitialised pointer.
00:06:08.040
A memory-safe language would require that references
00:06:11.198
are initialised, and require that they refer
00:06:14.356
to a valid data.
00:06:16.260
So it shouldn't be possible to have
00:06:20.259
an uninitialised reference, a reference that points
00:06:24.258
to nothing.
00:06:25.500
And languages like Rust enforce this.
00:06:27.814
You can't get a reference which is
00:06:30.513
uninitialised in Rust, it always points to
00:06:33.213
a valid value.
00:06:34.470
And, to be fair, good to C
00:06:36.913
compilers will warn about this behaviour.
00:06:39.007
If you turn on the warnings,
00:06:41.102
they’ll warn you, in most cases,
00:06:43.196
that you're accessing and uninitialised pointer.
00:06:45.390
But the C language doesn't require that
00:06:47.752
warning, and allows you to access arbitrary
00:06:50.113
pointers, whether or not they point to
00:06:52.475
valid memory.
00:06:56.220
Similarly, C and C++ allow you to
00:07:00.689
use memory after you freed it.
00:07:04.620
And this is a simple example.
00:07:07.234
It allocates 14 bytes of space,
00:07:09.849
writes the string “Hello, world!” into it,
00:07:12.899
frees that memory, and then prints out
00:07:15.949
the value.
00:07:16.920
And, in most cases, this will work,
00:07:20.760
because nothing else is using that memory,
00:07:23.829
because this is only a simple test
00:07:26.898
function with main() in it.
00:07:29.190
But it's this was a larger program
00:07:31.939
with concurrent accesses going on, that memory
00:07:34.689
could have been reused by another thread,
00:07:37.438
and it could print out who knows
00:07:40.187
what.
00:07:40.680
And what happens is entirely undefined.
00:07:44.432
Modern languages, with automatic memory management,
00:07:48.184
avoid this problem.
00:07:50.160
They either fail to compile, or they
00:07:52.489
ensure the memory is held for the
00:07:54.819
appropriate amount of time.
00:07:59.550
Equally, we can access memory implicitly after
00:08:02.442
it’s been freed. The typical example here
00:08:05.334
is a function which returns a reference
00:08:08.227
to stack allocated memory, and once the
00:08:11.119
function returns the stack frame is destroyed,
00:08:14.011
and the reference now points to.
00:08:16.590
unallocated memory. And, again, you've got an
00:08:20.402
arbitrary reference, and it's not clear what’s
00:08:24.214
in the memory which is being referred
00:08:28.026
by that reference.
00:08:29.760
And, again, languages with automatic memory management
00:08:32.577
eliminate this type of bug. Their either
00:08:35.395
hold onto the value so that it
00:08:38.212
can't go away if it's heap allocated,
00:08:41.130
if it's a language with boxed values,
00:08:43.135
or if it's a language like Rust
00:08:45.140
they just won’t compile if you're returning
00:08:47.145
a reference which goes out of scope.
00:08:52.800
Memory unsafe languages can allow memory to
00:08:57.172
be accessed as the wrong type.
00:09:01.020
This is a bit more subtle.
00:09:03.993
The code we have here
00:09:06.570
allocates space for a buffer and reads
00:09:09.674
data into that buffer, for example it
00:09:12.779
could be reading from a socket.
00:09:15.540
And, assuming it successfully returns into that
00:09:18.777
buffer, it then casts the buffer to
00:09:22.013
be a pointer to a different type.
00:09:25.350
And this is quite a common idiom
00:09:28.771
in C code. You see casts from
00:09:32.191
char* pointers representing buffers, to a certain
00:09:35.612
struct, for example, representing a network packet
00:09:39.032
format or representing the header of a
00:09:42.453
file format.
00:09:43.530
And the reason people do this is
00:09:46.339
because it's very efficient. There’s no memory
00:09:49.149
copies.
00:09:49.650
And, assuming that the layout struct matches
00:09:52.812
the layout of the data you want,
00:09:55.974
it's a really efficient way of getting
00:09:59.136
a reference to data in the appropriate
00:10:02.298
format.
00:10:02.850
The problem is that it's unsafe.
00:10:05.383
It makes assumptions that the layout of
00:10:08.338
the struct in memory matches the format
00:10:11.293
of the data.
00:10:12.660
And it makes assumptions that the size
00:10:15.689
of the block being cast, matches the
00:10:18.719
size of the struct.
00:10:20.550
And these assumptions have a habit of
00:10:24.090
becoming untrue over time.
00:10:28.080
They tend to work at the point
00:10:30.752
the programmer writes the code,
00:10:32.760
otherwise it would it would fail,
00:10:35.061
and wouldn't it wouldn't pass the tests
00:10:37.745
initially. But the problem is that compilers
00:10:40.429
tend to change the way structs are
00:10:43.113
laid out.
00:10:43.980
Changes in the compiler, or changes in
00:10:47.657
the processor, can insert padding between struct
00:10:51.335
elements.
00:10:51.960
And that means that the layout of
00:10:54.877
the struct no longer matches, and the
00:10:57.793
program silently fails and gives unpredictable behaviours.
00:11:00.810
Or, it turn out, the program is
00:11:04.014
run on a big-endian versus a little-endian
00:11:07.218
machine, and silently gives you the wrong
00:11:10.422
values.
00:11:10.980
And, because the type system is being
00:11:13.437
overridden, because the program is essentially saying
00:11:15.893
“trust me, I know what I’m doing”,
00:11:18.450
"pretend this set of bytes is a
00:11:21.298
value of this other type”, such changes
00:11:24.147
in layout, changes in behaviour, tend not
00:11:26.995
to get detected, and the program fails
00:11:29.843
silently when used on a different compiler,
00:11:32.692
or on a different type of machine.
00:11:35.640
And memory safe languages disallow such arbitrary
00:11:38.971
casts. They make you explicitly write the
00:11:42.302
conversion functions, and explicitly parse the buffer
00:11:45.632
into the struct.
00:11:47.160
And this is slightly slower, because you
00:11:49.728
have to explicitly go through and make
00:11:52.295
the copy, but it eliminates the undefined
00:11:54.863
behaviour and it's consistent.
00:11:56.430
It avoids silent failures when something changes.
00:12:04.620
Memory unsafe languages allow you to apply
00:12:08.282
string functions on non string values.
00:12:11.520
And this is a really common security
00:12:15.733
vulnerability in C programs,
00:12:18.240
where the C language assumes strings are
00:12:20.947
NUL terminated, there's a terminating zero value
00:12:23.653
at the end of a string,
00:12:25.973
but the recv() function, for reading from
00:12:28.680
a file or reading from the network,
00:12:31.387
doesn't add that terminating zero.
00:12:33.420
So program that's wanting to treat data
00:12:35.755
read from the network, or read from
00:12:38.089
a file, as a string, in C,
00:12:40.424
has to explicitly add at terminating zero
00:12:42.758
on to the end of the values
00:12:45.093
returned by the recv() call.
00:12:46.860
And if you forget, and call a
00:12:49.386
string function on that, and in this
00:12:51.913
case we're calling strlen() in such a
00:12:54.439
way,
00:12:54.900
the string length function just keeps reading
00:12:57.645
until it finds the first zero in
00:13:00.390
memory, and who knows what it reads
00:13:03.136
in memory.
00:13:04.020
And, maybe,
00:13:06.960
in the best case that just leaks
00:13:10.180
the contents of memory, maybe exposes some
00:13:13.400
sensitive data. And in the worst case,
00:13:16.620
it keeps going until it runs into
00:13:19.840
some unallocated memory and crashes the program.
00:13:23.160
And memory safe languages apply string bounds
00:13:27.614
checks.
00:13:28.350
They make sure that the types match,
00:13:30.557
they make sure you can't access over
00:13:32.764
the end of the string,
00:13:34.440
and they either succeed or, if they’re
00:13:37.272
going to fail, they fail with a
00:13:40.104
runtime exception which closes the program cleanly
00:13:42.936
without undefined behaviour.
00:13:49.830
Memory unsafe languages put no requirements that
00:13:55.495
you only access the memory you've allocated.
00:14:01.260
In a memory unsafe language you can
00:14:05.210
allocate, in this case, memory for 256
00:14:09.161
bytes of memory, but copying-in an arbitrary
00:14:13.111
sized buffer.
00:14:14.340
And the language, the strcpy() call,
00:14:17.393
and the way
00:14:19.020
arrays and pointers work in C,
00:14:22.119
means there’s no checks that the value
00:14:25.735
that's being copied into that buffer,
00:14:28.834
fits in within the 256 bytes allocated.
00:14:32.550
And it can overwrite the end of
00:14:34.646
the array, and it can corrupt whatever
00:14:36.743
happens to be in memory next.
00:14:38.640
And memory safe languages apply bounds checks.
00:14:43.710
If you try to overwrite the end
00:14:45.839
of an array in C, it will
00:14:47.969
just corrupt whatever's next memory.
00:14:49.590
If you try to overwrite the end
00:14:51.457
of an array in Java,
00:14:52.890
or in Rust, the program will fail,
00:14:56.524
but it will fail with an exception,
00:15:00.159
or a panic in Rust, which closes
00:15:03.793
down the program cleanly.
00:15:05.970
Which doesn't fail in an undefined way;
00:15:08.408
it has defined failure semantics. It doesn't
00:15:10.845
leak information, it doesn't corrupt the state,
00:15:13.283
it just cleanly shuts the program down.
00:15:18.780
And you can do the same thing
00:15:20.704
with arrays. The memory doesn't have to
00:15:22.628
be allocated on the heap for you
00:15:24.553
to be able to overflow it in
00:15:26.477
C or C++ and, again, there's no
00:15:28.401
bounds checks, whereas in a safe language
00:15:30.325
there should be.
00:15:34.560
Or you can perform arbitrary pointer arithmetic.
00:15:39.720
This C program
00:15:43.050
is calculating,
00:15:48.690
it's looking at the size of the
00:15:51.681
buffer, so the check for buf_ptr is
00:15:54.671
less than buf+sizeof(buf), looks like it's checking
00:15:57.662
the pointer is within range, it looks
00:16:00.652
like it's manually implemented about a bounds
00:16:03.643
check.
00:16:04.170
But the bug here is that the
00:16:07.337
buf_ptr is a pointer and int,
00:16:10.053
and sizeof() returns a size in bytes,
00:16:13.320
and what this program is doing,
00:16:16.198
when it increments buf_ptr, is incrementing by
00:16:19.555
int sized chunks, rather than byte sized
00:16:22.912
chunks, so the sizes don't match-up.
00:16:25.890
And it ends up with a buffer
00:16:29.225
overflow because it's doing
00:16:31.230
arithmetic on pointers, but it's not doing
00:16:34.625
the arithmetic incorrectly.
00:16:36.180
And, again, the language shouldn't allow
00:16:41.700
accesses where the types don't match up.
00:16:51.360
And memory unsafe languages allow uninitialised memory
00:16:56.057
to be accessed.
00:16:58.170
In this example, we're allocating space,
00:17:01.058
allocating one byte of memory to store
00:17:04.427
the headers.
00:17:05.490
And this is an example, it might
00:17:07.917
be reading HTTP headers, and is growing
00:17:10.344
a buffer, and reading the headers in,
00:17:12.772
byte-by-byte, and checking after each read() to
00:17:15.199
see whether it's received the complete set
00:17:17.626
of headers, if they end with a
00:17:20.053
double carriage return newline.
00:17:21.540
The problem is the initial allocation allocates
00:17:24.121
space one byte, but doesn't fill in
00:17:26.703
that memory.
00:17:27.540
It doesn't have an end-of-string marker there,
00:17:30.010
to say that this is an empty
00:17:32.480
string. So the first strstr() call just
00:17:34.950
runs off the end of the uninitialised
00:17:37.420
memory, and keep searching for the zero.
00:17:39.990
And it's similar to the bug we
00:17:42.943
saw earlier, with strings not being zero
00:17:45.897
terminated, but it happens because the language
00:17:48.850
lets you reference uninitialised memory.
00:17:51.060
The language lets you reference memory that
00:17:53.977
hasn't got a defined value.
00:17:56.160
And a memory safe language would require
00:17:59.579
that all the memory is initialised.
00:18:02.610
It would require that any allocation,
00:18:05.710
any heap allocation, has known contents.
00:18:08.910
Whereas the unsafe languages don't do that,
00:18:11.891
and they let you perform computations on
00:18:14.872
arbitrary, unpredictable, values.
00:18:21.660
And we have languages which allow you
00:18:25.082
to access
00:18:26.160
memory via dangling pointers, via null pointers.
00:18:31.320
which can access values where the pointer
00:18:34.952
points to
00:18:36.090
some unknown, unpredictable, value.
00:18:43.500
And you may get a null pointer
00:18:45.960
returned from lookup(), and try to dereference
00:18:48.419
it, and that will fail. Or you
00:18:50.879
may have a pointer to something which
00:18:53.339
is no longer existing and it may
00:18:55.799
fail.
00:18:59.130
And are more.
00:19:01.770
That’s a
00:19:03.930
long summary of some of the more
00:19:06.603
common types of memory unsafe behaviour,
00:19:08.894
but the link has many dozens more
00:19:11.568
types of memory unsafe behaviours that can
00:19:14.241
happen in C or C++.
00:19:22.590
And the issue here is that lack
00:19:26.268
of memory safety breaks the abstractions.
00:19:29.520
It means the program starts doing unpredictable
00:19:35.260
things.
00:19:36.180
And, if you're lucky, the program just
00:19:38.923
crashes, and crashes early and visibly,
00:19:41.273
and you can tell that there's a
00:19:44.016
memory safety bug, and you can fix
00:19:46.758
it.
00:19:47.250
If you're unlucky,
00:19:51.150
the program accesses memory which it doesn't
00:19:54.637
own, or accesses memory in a way
00:19:58.123
that doesn't match the types, and it
00:20:01.610
silently corrupts some other value.
00:20:04.200
It silently corrupts some other value stored
00:20:07.902
in memory.
00:20:09.060
And maybe that's memory which isn't being
00:20:11.589
used, and the corruption has no effect.
00:20:14.117
But maybe it's memory which is being
00:20:16.646
used, and is storing some other value
00:20:19.174
in the program, and you've just silently
00:20:21.703
corrupted the value of another variable,
00:20:23.970
with no way of knowing when,
00:20:26.779
or where, that change happened.
00:20:29.220
And you can't predict what's going to
00:20:31.510
happen without knowing the exact layout of
00:20:33.800
the program in memory. And, if it's
00:20:36.090
a multi-threaded program, knowing the exact order
00:20:38.380
of execution of all the different threads.
00:20:40.770
It’s very difficult to debug such behaviours,
00:20:44.136
and it’s potentially a security risk because
00:20:47.501
by corrupting the program state, and attacker
00:20:50.867
can cause the program
00:20:52.890
to do something which wasn't intended,
00:20:56.020
and potentially force arbitrary code execution.
00:21:04.020
And this is a real problem.
00:21:07.860
The chart we have on the left,
00:21:11.203
which is a little hard to read
00:21:14.546
perhaps, is looking at the number of
00:21:17.889
reported security vulnerabilities, for a 20 year
00:21:21.232
period.
00:21:21.810
And this is
00:21:23.670
a list of all the CVEs,
00:21:27.930
the common vulnerability database, and it's a
00:21:31.451
list of all of the security vulnerabilities
00:21:34.973
reported, in all software, over a 20
00:21:38.494
year period.
00:21:39.600
And the column highlighted
00:21:42.300
is showing the vulnerabilities which are due
00:21:45.735
to buffer overflows, memory corruption, or treating
00:21:49.170
data as executable code. It’s highlighting vulnerabilities
00:21:52.606
which are due to memory unsafety.
00:21:55.650
If you look at the numbers,
00:21:58.245
we see that about half of all
00:22:01.273
security vulnerabilities are memory unsafety vulnerabilities.
00:22:03.868
They are problems due to lack of
00:22:06.895
memory safety in the language, which should
00:22:09.922
be caught by a modern type system.
00:22:13.050
The compiler should be able to detect
00:22:17.686
these problems.
00:22:19.110
And a lot of the others,
00:22:21.840
I think, by modelling the problem domain
00:22:24.512
in the types, even if they're not
00:22:27.183
directly related to memory unsafety, the compiler
00:22:29.855
can also help check for these types
00:22:32.527
of problems.
00:22:33.390
Things like SQL injection bugs,
00:22:37.920
parsing bugs, the type system can help
00:22:40.732
check for consistency, can help check that
00:22:43.543
these are not happening.
00:22:48.870
And this is a another graph,
00:22:51.486
this is from a presentation by Microsoft,
00:22:54.539
and it's looking at the type of
00:22:57.591
security vulnerabilities in Microsoft software, again over
00:23:00.644
a 10 year or so period.
00:23:03.360
And, in Microsoft's case,
00:23:06.030
about 70% of their security updates fix
00:23:10.176
bugs relating to unsafe memory usage.
00:23:13.830
And this is perhaps a little higher
00:23:16.085
than the previous graph, because of the
00:23:18.341
type of software that Microsoft develops,
00:23:20.274
which is a lot of systems code,
00:23:22.529
a lot of applications code,
00:23:24.240
whereas the previous graph, the previous table,
00:23:28.588
was looking at all software, including web
00:23:32.936
applications, and so on.
00:23:35.520
I think what's interesting is that we're
00:23:38.892
not getting better at this.
00:23:41.400
We have known that memory safety bugs
00:23:43.979
are a problem for a very,
00:23:46.189
very long time, 10-20 years, and we're
00:23:48.768
not getting better at writing secure code
00:23:51.346
in memory unsafe languages.
00:23:52.920
The number of memory safely bugs isn't
00:23:55.467
going down. And it's not that Microsoft
00:23:58.013
doesn't know that this is a problem.
00:24:00.660
It's just that this is a hard
00:24:03.062
problem, and it's difficult to fix in
00:24:05.464
these languages.
00:24:11.580
What should you do as a systems
00:24:15.981
programmer?
00:24:16.710
If you have to write in a
00:24:20.168
memory unsafe language,
00:24:21.750
use the modern tooling.
00:24:25.470
If you're compiling C code, at the
00:24:28.912
very minimum, turn on all the warnings
00:24:32.355
you can find.
00:24:33.930
The minimum set of flags for compiling
00:24:37.612
C code, if you're using clang,
00:24:40.768
for example, should be -W -Wall -Werror.
00:24:44.550
And you’d think this would turn on
00:24:46.548
all warnings, and make them errors.
00:24:48.360
But, actually what it does, is turn
00:24:50.745
on all warnings that a particular version
00:24:53.130
of GCC had, and make them errors.
00:24:55.515
So look at the documentation, and find
00:24:57.900
the additional warnings, and see if it
00:25:00.286
makes sense to turn them on.
00:25:02.430
Find the warnings which exist in addition
00:25:06.269
to -Wall, and turn them on.
00:25:09.660
Fix all the warnings. When the compiler
00:25:12.318
says there's a warning, treat it as
00:25:14.976
an error; force the compiler to treat
00:25:17.634
it as an error. Let the compiler
00:25:20.292
help you debug your code.
00:25:22.290
And when you're debugging your code,
00:25:24.897
when you're testing your code, use the
00:25:27.938
static analysis tools that exist.
00:25:30.210
And this could be older tools like
00:25:32.893
valgrind, or it could be more modern
00:25:35.577
tools, like the address and the memory
00:25:38.260
sanitisers, or the undefined behaviour sanitisers,
00:25:40.560
the thread sanitisers, built into clang.
00:25:42.960
There's a lot of tools that exist
00:25:45.645
now that perhaps have a very high
00:25:48.329
overhead, perhaps can’t be used in production,
00:25:51.014
but for debugging and testing can help
00:25:53.698
you catch many memory and thread related
00:25:56.383
safety problems.
00:26:03.240
If you're using C++, use the modern
00:26:07.739
features of the language.
00:26:10.410
The modern C++ idioms are much safer
00:26:13.764
than the older idioms.
00:26:15.780
Although, in general, I have to say
00:26:18.426
I would advise against C++ programming because
00:26:21.073
it's too complicated. The problem, I think,
00:26:23.719
with C++ is that features keep getting
00:26:26.366
added over time, and features don't get
00:26:29.012
removed.
00:26:29.490
So modern C++ is quite a nice,
00:26:32.558
quite a safe, language, if you can
00:26:35.625
understand it, and if you can make
00:26:38.693
sure that the code doesn't use any
00:26:41.761
of the older, less safe, features.
00:26:44.490
But the problem with C++ is that
00:26:47.977
very few people know the whole language,
00:26:51.463
it made some bad choices in early
00:26:54.950
versions, had some inappropriate defaults,
00:26:57.540
and you just need a lot of
00:27:00.184
knowledge to write C++ code correctly,
00:27:02.450
know which features are safe to use,
00:27:05.093
know in which cases you need to
00:27:07.737
override the defaults.
00:27:08.970
It’s getting better at that, the modern
00:27:10.971
versions solve a lot of these issues,
00:27:12.973
but they have to retain backwards compatibility,
00:27:14.974
so they have to retain all the
00:27:16.975
unsafe features. The means you need to
00:27:18.976
audit all the code
00:27:20.220
to check that they're not being used,
00:27:22.801
and you need to understand all of
00:27:25.382
those features.
00:27:26.220
One of the reasons I think Rust
00:27:28.704
is a good choice for systems programming
00:27:31.188
is that it's learned from this experience,
00:27:33.672
and it's looked at what works in
00:27:36.156
C++ and what, with the benefit of
00:27:38.640
hindsight, doesn't.
00:27:39.450
And, because it's a new language,
00:27:41.277
it doesn't need to be backwards compatible,
00:27:43.409
so it can fix the problems C++
00:27:45.541
can't fix.
00:27:49.770
Ultimately, I think, if you have a
00:27:52.634
memory unsafe code base, you should be
00:27:55.499
looking to gradually rewrite it, or at
00:27:58.363
least rewrite the critical sections, the input
00:28:01.227
parsing code for example, into a memory
00:28:04.092
safe language.
00:28:05.010
If you have C or C++ code,
00:28:07.841
you can gradually rewrite it into Rust,
00:28:10.671
for example.
00:28:11.580
And
00:28:13.320
one of the nice things about Rust,
00:28:15.933
is that you can compile it in
00:28:18.547
a way that the functions are compatible
00:28:21.160
with C code.
00:28:22.380
So you can rewrite a program function
00:28:25.015
at a time, and link in the
00:28:27.649
Rust code, and gradually convert it over
00:28:30.284
a period of many years, function by
00:28:32.919
function, testing it as you go,
00:28:35.177
and gradually
00:28:36.030
fixing the problems.
00:28:39.270
And the links on the slide point
00:28:41.709
to some blog posts that talk about
00:28:44.147
how to do this. And there’s been
00:28:46.586
a couple of examples of open source
00:28:49.025
projects which have done this, and have
00:28:51.463
gradually transitioned
00:28:52.260
a code base from C to Rust
00:28:55.753
over a five year period, for example,
00:28:59.246
while keeping the API, keeping compatibility.
00:29:02.340
If you're in the Apple world,
00:29:04.498
you can do the same sort of
00:29:07.015
thing, gradually rewriting from Objective C to
00:29:09.533
Swift, for example, and get the safety
00:29:12.050
benefits.
00:29:12.510
But this is difficult. IT requires languages
00:29:15.833
that have compatible runtime models, so you
00:29:19.156
can link the code together,
00:29:21.630
and write a program in multiple languages,
00:29:24.438
and it requires that you have programmers
00:29:27.246
who know both languages.
00:29:28.950
But I think this sort of gradual
00:29:31.548
rewrite is the way to go,
00:29:33.775
and has much more chance of success
00:29:36.374
than a from-scratch rewrite, because you can
00:29:38.972
keep testing the program as go,
00:29:41.199
you can keep bugging the program as
00:29:43.798
you go,
00:29:44.640
rather than having to throw it all
00:29:47.051
away, start from scratch, and have to
00:29:49.461
reimplement everything.
00:29:53.760
That’s all I want to say about
00:29:56.592
memory safety. We've seen a small fraction
00:29:59.425
of the long list of undefined behaviours
00:30:02.257
that could happen in C and C++,
00:30:05.190
and hopefully this gives you a flavour
00:30:08.431
of why these languages are dangerous.
00:30:11.209
We spoke about the security impact of
00:30:14.450
memory unsafety, and the way 50-70% of
00:30:17.692
all security vulnerabilities in the code are
00:30:20.933
due to memory unsafety, and this isn't
00:30:24.174
getting better.
00:30:25.200
And I spoke a little bit about
00:30:27.954
the mitigations, the idea that we should
00:30:30.709
be gradually transitioning to memory safe languages,
00:30:33.463
where possible.
00:30:34.350
And I think that the number of
00:30:36.894
security vulnerabilities make it clear that,
00:30:39.075
ultimately, the way forward has to be
00:30:41.619
to transition to memory safe languages.
Part 2: Parsing and Network Security
The second part of the lecture discusses security vulnerabilities in
networked applications. It highlights the problem of how to parse
untrusted input as critical, and discusses some of the implications
of Postel's Law on the security of input parsers. It discusses some
approaches to building safe parsers, and introduces some of the ideas
from the LangSec movement.
00:00:00.000
In this part, I want to talk
00:00:02.374
about parsing and network security.
00:00:04.170
I'll talk about some of the issues
00:00:06.327
with handling untrusted input data, such as
00:00:08.484
the type of input you get from
00:00:10.641
the network. I'll talk about Postel’s law,
00:00:12.798
and I’ll talk about approaches to building
00:00:14.956
more robust parsers.
00:00:18.660
As we saw in the previous part
00:00:21.773
of the lecture, writing safe code in
00:00:24.886
memory unsafe languages is difficult.
00:00:27.210
There's a large number of perhaps somewhat
00:00:30.658
subtle vulnerabilities, somewhat subtle bugs, that can
00:00:34.106
occur, which lead to undefined behaviour,
00:00:37.061
lead to unsafe behaviour, and those bugs
00:00:40.509
can lead to exploitable flaws in the
00:00:43.957
code.
00:00:44.550
And the problem with a lot of
00:00:46.620
this, is that it’s easy to look
00:00:48.689
at each of these individual bugs and
00:00:50.759
rationalise them.
00:00:51.450
It's easy to look at these types
00:00:53.988
of problems, and say “how could anyone
00:00:56.527
possibly write code like that?”
00:00:58.440
And the problem is that a lot
00:01:01.339
of these security vulnerabilities, a lot of
00:01:04.237
the remotely exploitable vulnerabilities we see in
00:01:07.136
network systems, fall under this category.
00:01:09.720
And the example we see on the
00:01:13.784
slide here, is a real security vulnerability
00:01:17.847
from the Apple TLS code.
00:01:20.850
And what's happened here is clearly a
00:01:24.248
mismerge, or misedit, such that a particular
00:01:27.645
line has got duplicated, and the line
00:01:31.043
“goto fail”, has got duplicated.
00:01:33.570
The problem, of course, is that
00:01:36.690
because indentation is not significant in C,
00:01:39.910
what this does is always goes to
00:01:43.130
the failure case, and skips a bunch
00:01:46.350
of checks.
00:01:47.370
And it led to a particular check
00:01:50.078
being skipped, and that led to code
00:01:52.787
that was remote remotely exploitable, because the
00:01:55.495
checks that validated the input were not
00:01:58.203
performed, and that then led to a
00:02:00.912
memory unsafety bug further down the line.
00:02:03.720
And it's easy to look at examples
00:02:06.039
like this, and say “how could anyone
00:02:08.358
write that?”
00:02:09.120
How could the programmer not spot that
00:02:11.691
this was a bug?
00:02:13.260
Of course, for any individual
00:02:16.110
bug you can do that, you can
00:02:18.492
look at it and say, well,
00:02:20.535
of course, you should have seen that.
00:02:22.917
But, the thing is, people keep making
00:02:25.300
mistakes.
00:02:25.740
We’ve ended up in a situation where
00:02:28.678
we're expecting programmers to be perfect.
00:02:31.197
We're expecting programmers to never make mistakes.
00:02:34.135
And not providing the tools to help
00:02:37.073
them, not providing the tools to catch
00:02:40.012
the mistakes when they get made.
00:02:42.630
And the problem is these types of
00:02:45.750
memory on safety bugs lead to remotely
00:02:48.870
exploitable vulnerabilities. They lead to situations where
00:02:51.989
attackers can use that undefined behaviour,
00:02:54.663
can figure out what happens in particular
00:02:57.783
cases, and can break into systems and
00:03:00.903
run arbitrary code.
00:03:02.340
And this is, of course, a problem.
00:03:06.390
If we want to improve security,
00:03:08.850
I think we need to focus on,
00:03:11.820
as we spoke about last time,
00:03:13.754
we need to focus on getting rid
00:03:16.011
of memory unsafe languages.
00:03:17.400
But obviously this is a long term
00:03:19.780
goal.
00:03:20.220
And it may make sense to think
00:03:22.501
about are there particular types of bug,
00:03:24.781
particular classes of bug, which most cause
00:03:27.062
such vulnerabilities, which we can address in
00:03:29.343
the short term.
00:03:30.420
Where we can expand the effort to
00:03:33.593
proactively move to safer languages, or to
00:03:36.767
proactively use new techniques, or to proactively,
00:03:39.940
at least, review the code carefully.
00:03:45.990
Well, I think there are. And I
00:03:48.327
think one of these areas where
00:03:50.430
we need to proactively spend this effort,
00:03:53.164
is parsing untrusted input. The input from
00:03:55.898
the network to the rest of the
00:03:58.632
program, or the input from a failed
00:04:01.366
parser, to the rest of the program.
00:04:04.200
And, if we focus on networked systems,
00:04:06.704
the structure of a networked system,
00:04:08.850
in a very high level terms,
00:04:10.996
is that you get some untrusted protocol
00:04:13.499
data from the network.
00:04:15.030
You need to parse it, take the
00:04:17.785
protocol messages, parse them into some internal
00:04:20.640
data structure.
00:04:23.040
And the protocol code then operates on
00:04:26.236
those data structures, and serialises the results
00:04:29.432
for transmission back out across the network,
00:04:32.627
in response to the request.
00:04:35.010
I think what should be clear,
00:04:36.942
is that the input parser is critical
00:04:39.196
to the security of any networked system.
00:04:41.550
It’s taking untrusted input data from the
00:04:44.067
network, where we know there are people
00:04:46.584
who are maliciously trying to break the
00:04:49.100
systems,
00:04:49.560
and it's trying to generate validated,
00:04:51.897
strongly-typed, data structures that can be processed
00:04:54.623
by the rest of the code.
00:04:57.060
And one of the key challenges is
00:05:00.075
ensuring that the parsers are safe.
00:05:02.760
And ensuring that parsers safe, even if
00:05:05.517
they've written in memory unsafe languages.
00:05:11.070
So how do we build networked systems?
00:05:14.760
Well, the traditional guidance, when writing networked
00:05:18.165
systems, has been expressed in the form
00:05:21.571
of Postel’s law.
00:05:23.130
And Postel’s law was first described in
00:05:26.482
RFC 1122.
00:05:27.540
And it, it talks about interoperability and
00:05:32.860
robustness of network protocols.
00:05:36.000
And the suggestion is that you should
00:05:38.583
be liberal in what you accept,
00:05:40.796
and conservative in what you send.
00:05:43.110
That is, when generating data, you should
00:05:45.807
try your hardest to make sure the
00:05:48.503
data you generate conforms to the protocol.
00:05:51.300
But when parsing data that you receive
00:05:54.042
from the network, the suggestion from Postel’s
00:05:56.784
law is that you should be forgiving
00:05:59.526
of errors, you should be forgiving of
00:06:02.268
the
00:06:02.760
precise details of the format, providing you
00:06:07.885
can make sense of what you get.
00:06:13.110
The software does need to be rewritten
00:06:16.780
to deal with errors, it does need
00:06:20.450
to cope with the fact that the
00:06:24.120
data is coming in which is malevolent,
00:06:27.790
which is designed to break the system,
00:06:31.560
but if it's too strict, too unforgiving,
00:06:37.012
interoperability suffers.
00:06:38.670
And there's a balance. And Postel’s law
00:06:41.477
is saying, err
00:06:42.780
towards being liberal, towards accepting
00:06:50.400
data which is not necessarily well-formed,
00:06:53.620
to enhance interoperability.
00:06:58.080
I think the question is whether this
00:07:00.821
principle of being liberal in what you
00:07:03.563
accept, conservative in what you send,
00:07:05.912
is still appropriate for today's network,
00:07:08.262
and for today's networked systems?
00:07:10.320
And I think it's not clear that
00:07:12.202
it is.
00:07:12.840
Paul-Henning Kamp, one of the FreeBSD developers,
00:07:16.995
expressed this in the
00:07:19.470
clearest way I’ve seen, when he said
00:07:21.939
that Postel lived on a network with
00:07:24.409
all of his friends,
00:07:25.920
and we today live on a network
00:07:29.045
with our enemies. People are trying to
00:07:32.170
break the systems, people are trying maliciously
00:07:35.295
to break our code, break our applications.
00:07:38.520
Postel’s law, I think, is not appropriate
00:07:42.482
for today's Internet.
00:07:44.280
We should not, perhaps, be so liberal
00:07:47.041
in what we accept. We should clearly
00:07:49.801
specify what is legal. We should clearly
00:07:52.562
specify what the code will accept,
00:07:54.928
and what it won't accept. And we
00:07:57.688
should be strict about that.
00:07:59.760
We should be strict about how,
00:08:02.127
and when, our code fails.
00:08:04.200
Because the consequences of being too liberal
00:08:07.738
is that our systems are exploitable.
00:08:10.770
If we don't precisely specify that behaviour,
00:08:14.307
there are too many corner-cases with undefined
00:08:17.845
behaviour.
00:08:18.450
The Internet Architecture Board, in the IETF,
00:08:21.339
also has some discussion of this,
00:08:23.816
and the draft I’ve linked.
00:08:29.370
When we're building network systems, the approach
00:08:33.411
we should be taking
00:08:35.820
is to clearly specify what is legal,
00:08:41.084
what is not.
00:08:43.440
Input parsers are designed to take in
00:08:47.207
untrusted data, validate it, and generate strongly-typed,
00:08:50.973
safe, data which we can then use.
00:08:54.840
They’re taking something which is untrusted and
00:08:57.508
unsafe, and converting it to a form
00:09:00.177
which can be trusted, and which is
00:09:02.845
safe, which is usable.
00:09:04.470
And they do that by parsing the
00:09:08.052
input according to some grammar.
00:09:10.710
And if the input doesn't match the
00:09:13.663
grammar, the parser fails.
00:09:15.450
And the problem, I think, with a
00:09:17.720
lot of traditional protocols, and a lot
00:09:19.989
of traditional parsers, is that it hasn't
00:09:22.259
been clear what that grammar is,
00:09:24.205
and what are the failure conditions.
00:09:26.250
And this is the problem of Postel’s
00:09:29.328
law. It encourages each application to extend
00:09:32.406
the grammar in arbitrary ways, to be
00:09:35.484
a little bit flexible, a little bit
00:09:38.562
forgiving of giving of different inputs,
00:09:41.201
malformed inputs.
00:09:42.180
And that leads to inconsistencies, systems which
00:09:45.272
work in some cases, which
00:09:47.580
don't work in other cases.
00:09:52.050
And those inconsistencies in behaviour leads to
00:09:57.055
gaps, where particular implementations can be exploited.
00:10:02.160
So, we should define what's legal,
00:10:04.105
and what's not, and write this down
00:10:06.374
into machine readable way.
00:10:07.770
Ideally, we should do this in the
00:10:10.584
protocol specification, the protocol standard, so every
00:10:13.399
implementation can use the exact same grammar,
00:10:16.213
and accepts the exact same inputs,
00:10:18.625
and there's no ambiguity, non inconsistency,
00:10:21.038
in what's acceptable and what isn't.
00:10:23.550
And Ideally, we should do this in
00:10:26.428
as restrictive a manner as possible.
00:10:28.896
The grammar should be as restricted as
00:10:31.774
it's feasible to do so.
00:10:33.930
Because the more expressive power we have,
00:10:36.305
the more likely we are to have
00:10:38.681
risks,
00:10:39.120
the more scope for vulnerabilities, the more
00:10:43.859
scope for ambiguity.
00:10:45.990
And importantly, we should specify what happens
00:10:48.370
if the data doesn't match the grammar.
00:10:50.850
We should specify what causes of failure,
00:10:53.164
and how failures are handled. And,
00:10:55.147
ideally, we should do so in the
00:10:57.461
standard. But if the standard doesn't do
00:10:59.775
this, every implementation has to do it
00:11:02.089
itself.
00:11:02.520
It has to be clear what it's
00:11:05.406
accepting, and what it's not accepting.
00:11:13.560
And once we've done this, we should
00:11:17.597
automatically generate the parsing code.
00:11:20.580
We should automatically generate the parsing code,
00:11:23.209
using the simplest possible parsing technique.
00:11:25.462
If we've got a regular input language,
00:11:28.091
we should use a regular expression.
00:11:30.344
If we've got a context-free grammar we
00:11:32.972
should use a context-free parser.
00:11:34.950
If need be we should use more
00:11:37.515
sophisticated parsing techniques, but we should use
00:11:40.080
those of the minimal computational power to
00:11:42.645
minimise the ambiguity, minimise the risks,
00:11:44.844
if we mess up.
00:11:46.410
And we should generate strongly-typed data structures,
00:11:48.928
with explicit types to identify the different
00:11:51.446
types of data, so we know what
00:11:53.964
has been parsed, we know that it
00:11:56.482
conforms to a particular legal set of
00:11:59.000
values.
00:11:59.460
And we should do this automatically using
00:12:02.709
tools which can be validated.
00:12:05.130
And a lot of the problem here
00:12:07.344
is that people try to write parsers
00:12:09.558
by hand.
00:12:10.290
And manually written, ad-hoc, parsing code is
00:12:13.662
very difficult to reason about.
00:12:16.170
It tends to perform low-level bit manipulation.
00:12:19.301
It tends to perform detailed string parsing.
00:12:22.432
And all of this is hard to
00:12:25.563
get right, hard to structure.
00:12:27.900
But we know about parsers. There's a
00:12:31.135
whole theory of how to design parsers,
00:12:34.470
and grammar hierarchy, which you see on
00:12:37.510
the right hand side of the slide,
00:12:40.650
which talks about expressivity of the different
00:12:43.072
types of parsers. And there’s a whole
00:12:45.494
bunch of techniques for structuring parsers.
00:12:47.670
So we can write down a grammar,
00:12:50.455
we can formally verify the grammar,
00:12:52.842
we can formally generated a parser automatically
00:12:55.628
from that grammar, and we know that
00:12:58.413
it's consistent, and that it’s correct.
00:13:00.900
And I think the key goal is
00:13:03.228
to focus on parser correctness and readability.
00:13:05.556
Performance matters much less than getting this
00:13:07.884
right, getting this to be secure.
00:13:13.260
One of the key things we can
00:13:15.873
do to improve the security of systems
00:13:18.487
code,
00:13:18.960
is use existing, well-tested, parser libraries.
00:13:23.065
And use them as the input side
00:13:27.854
of our networked applications.
00:13:30.690
If you're writing Rust code, use something
00:13:34.588
like nom or combine. Well-tested, well-structured,
00:13:37.929
parser combinator libraries, which let you
00:13:41.370
easily specify the grammar, easily specify the
00:13:44.816
parsing rules.
00:13:45.900
If you're using C or C++,
00:13:49.144
use the Hammer parser which, again,
00:13:52.389
is designed to be robust, designed to
00:13:56.174
be testable and to fail in structured,
00:13:59.959
well-defined, ways.
00:14:01.140
Read this paper, “Writing Parsers like it
00:14:04.443
is 2017”, and this shows how this
00:14:07.746
is not a new idea, which talks
00:14:11.049
about how to make robust parsers.
00:14:17.760
Specify the types into which the data
00:14:21.050
is parsed, describe the parser using a
00:14:24.340
formal language, generate the parser from that
00:14:27.630
language.
00:14:28.200
And ensure that the parsing is performed
00:14:31.371
first, and either succeeds or fails in
00:14:34.542
its entirety. And then, if it succeeds,
00:14:37.712
you have safe, pre-parsed, structured, strongly-typed,
00:14:40.430
data on which to build the rest
00:14:43.601
of the code.
00:14:45.060
Input parsers are difficult.
00:14:49.020
They’re very difficult to get right,
00:14:50.909
if written in an ad-hoc way.
00:14:52.797
And, especially, if written in a low-level
00:14:55.001
memory unsafe language, because they involve lots
00:14:57.204
of bit manipulation, lots of string handling,
00:14:59.407
which are the features which are the
00:15:01.611
hardest in those languages.
00:15:02.970
Yet we have tools, we have parser
00:15:05.233
generator tools, so let's make use of
00:15:07.497
them.
00:15:07.920
Even if we're still using C or
00:15:10.090
C++, we've got tools that can make
00:15:12.260
this safer.
00:15:17.400
And when we're designing
00:15:19.470
network protocols, we should be thinking about
00:15:23.453
ease of parsing.
00:15:25.260
We should be thinking about minimising the
00:15:28.040
amount of state, the amount of look-ahead
00:15:30.819
required to parse the protocol data.
00:15:33.201
We should be thinking about the complexity
00:15:35.981
of the format, and how easy it
00:15:38.760
is to parse, or not to parse.
00:15:41.640
We should be designing network protocols,
00:15:43.985
such they are predictable, such that they
00:15:46.720
can be parsed using regular expressions,
00:15:49.065
or context free grammars, rather than needing
00:15:51.900
context, state-dependent parsing, because that allows us
00:15:55.336
to use simpler parser generators, and simpler
00:15:58.772
parsing code, and reduce the chances of
00:16:02.207
there being bugs.
00:16:03.780
The benefit of saving a few bits,
00:16:06.831
by having a more sophisticated, more complex,
00:16:09.882
format goes down over time, because the
00:16:12.933
networks get faster.
00:16:14.340
The security vulnerabilities remain. We should be
00:16:16.494
aiming for simplicity and ease of parsing,
00:16:18.648
and we should be using the best
00:16:20.803
parsing tools we can to get rid
00:16:22.957
of these vulnerabilities.
00:16:29.250
And I would urge you to read
00:16:32.535
this paper, “The Bugs We Have to
00:16:35.821
Kill”,
00:16:36.390
which talks much more in detail about
00:16:39.607
these ideas. It talks about this approach,
00:16:42.824
of using parser generator tools, modern languages,
00:16:46.041
strong type systems.
00:16:47.520
I think we can we can massively
00:16:50.132
improve network security by doing this.
00:16:52.470
We can improve network security by using
00:16:55.485
these techniques, even in unsafe languages.
00:16:58.170
So if you still have to write
00:17:00.582
C or C++ code, think how you
00:17:02.994
write the input parser, think about using
00:17:05.405
a
00:17:05.850
well-tested, well-structured, parsing tool, like Hammer.
00:17:08.942
Right down the grammar, write down the
00:17:12.549
failure conditions.
00:17:13.680
And, ideally, yes, implement this in a
00:17:15.780
safe language, but even if you're not,
00:17:17.880
we have better tools, we just need
00:17:19.980
to use them.
00:17:25.920
That’s all I want to say about
00:17:27.486
parsing.
00:17:27.810
Network security is hard.
00:17:31.020
It is hard to write safe parsers.
00:17:34.950
But we have tools, we have approaches,
00:17:37.988
which can help.
00:17:39.390
I would urge you,
00:17:41.820
whether or not you're using Rust,
00:17:44.460
or C, or C++ ,or Python,
00:17:47.100
or any other language, write down the
00:17:50.180
grammar which your code is accepting,
00:17:52.820
think about what's legal think, about the
00:17:55.900
failure conditions.
00:17:56.880
Define your parser in a robust,
00:17:59.130
structured, way.
Part 3: Modern Type Systems and Security
The final part of the lecture discusses some non-memory safety related
causes of security vulnerabilities in networked applications. It notes
that these often result from a failure to make assumptions explicit in
code, allowing an attacker to exploit inconsistent behaviour to cause
a program to do something unexpected. The implications of type-driven
development in strongly typed languages for forcing assumption to be
made explicit, and in checking designs for consistency, is noted, as
a likely driver to reduce the likelihood that vulnerabilities exist.
It's noted that memory safety, the use of strongly typed languages, and
type driven design won't eliminate security vulnerabilities, but they
do have the potential to reduce their prevalence.
00:00:00.000
In this final part of the lecture,
00:00:02.335
I want to look about modern type
00:00:04.669
systems, and how they can help build
00:00:07.004
secure systems.
00:00:07.771
I’ll talk about how modern type systems
00:00:09.915
help us make some of the assumptions
00:00:12.058
in our code explicit, and how they
00:00:14.202
help us eliminate undefined behaviour, and how
00:00:16.345
this can help improve the security of
00:00:18.489
networked systems.
00:00:22.531
So what causes security vulnerabilities in systems?
00:00:28.291
Well, they tend to be caused
00:00:31.921
by
00:00:33.721
the attacker persuading the program to do
00:00:37.932
something which the programmer didn't expect.
00:00:41.641
Security vulnerabilities occur when the attacker can
00:00:45.881
persuade the program to violate it’s assumptions.
00:00:50.221
If you can persuade the program to
00:00:52.439
write past the end of a buffer,
00:00:54.657
in a memory unsafe language,
00:00:56.341
and corrupt the state, which can then
00:00:59.100
cause it to behave in an unexpected
00:01:01.858
way. If you can persuade the program
00:01:04.617
to treat user input data as executable
00:01:07.375
somehow, by misusing using escape characters for
00:01:10.134
example. If you can confuse a permission
00:01:12.892
check somehow. Or any other type of
00:01:15.751
behaviour which the attacker can force the
00:01:19.568
program to do, which the programmer isn't
00:01:23.386
expecting.
00:01:24.031
Essentially, the goal of an attacker,
00:01:26.029
the goal of someone who is trying
00:01:28.360
to break into a networked system,
00:01:30.357
is to violate the assumptions in the
00:01:32.688
code.
00:01:33.121
To confuse the program into doing something
00:01:36.863
which is not expected to do.
00:01:40.171
A consequence of that, is that anything
00:01:43.498
we can do to make the assumptions
00:01:46.824
explicit, and check those assumptions, helps avoid
00:01:50.151
security vulnerabilities.
00:01:54.361
A lot of the reason why I’ve
00:01:58.078
been highlighting, and emphasising, strong typing,
00:02:01.263
is that strong types make the assumptions
00:02:04.980
explicit.
00:02:05.611
They let the compiler check that what
00:02:10.406
we're doing in the program makes sense.
00:02:15.301
And for this reason, I would argue
00:02:19.546
that strong typing helps reduce the security
00:02:23.792
vulnerabilities in code.
00:02:25.711
By using explicit types rather than generic
00:02:28.574
types, by defining conversion functions, by using
00:02:31.437
the type system to add semantic tags
00:02:34.301
to the data to help us understand
00:02:37.164
what it means,
00:02:38.491
we can be clear how the data
00:02:41.594
should be processed. We can be clear
00:02:44.698
what the data is, what has been
00:02:47.801
done to it,
00:02:49.231
and what are the legal operations on
00:02:52.302
that data. And by clearly specifying the
00:02:55.372
legal operations, we can have the compiler
00:02:58.443
help us check that illegal operations are
00:03:01.514
not performed..
00:03:02.491
And this, I think, is key to
00:03:05.145
improving the security of systems.
00:03:07.141
Most security vulnerabilities are not complex.
00:03:11.996
They’re not subtle, magic, misbehaviours.
00:03:16.141
They’re simple things, simple checks which have
00:03:19.983
been forgotten.
00:03:21.181
And the more we can express the
00:03:23.234
desired behaviour in the code, the more
00:03:25.288
the compiler can help us make sure
00:03:27.341
that we don't forget those checks.
00:03:33.721
When dealing with data, you should prefer
00:03:36.921
explicit types. A lot of the reason
00:03:40.120
for this, is because vulnerabilities come from
00:03:43.320
inconsistent data processing.
00:03:44.791
It's easy to accidentally take data which
00:03:47.782
arrives off the network,
00:03:49.591
and pass it, un-sanitised, to a function
00:03:53.142
which processes that data and execute that
00:03:56.694
data.
00:03:57.301
The XKCD cartoon, which is pretty well-known,
00:04:00.643
on the slide, is an example of
00:04:03.985
this, where you're taking some input data,
00:04:07.327
in this case the student’s name,
00:04:10.291
and accidentally passing it to a function
00:04:13.038
of expects valid SQL data. And because
00:04:15.786
the student’s name has a valid SQL
00:04:18.534
command embedded within it, it gets executed
00:04:21.281
and corrupts the database.
00:04:22.951
And this type of bug happens,
00:04:26.195
because we're conflating different types of data.
00:04:29.980
Because we're conflating, in this example,
00:04:33.225
a person's name with a set of
00:04:37.010
SQL commands.
00:04:38.191
But student names, and input data,
00:04:41.966
and SQL data, are different types.
00:04:45.841
And it should be possible to write
00:04:49.104
the code in a way that uses
00:04:52.367
one type of value to represent untrusted
00:04:55.630
input data, and another data type to
00:04:58.893
represent SQL commands.
00:05:00.391
And to have a specific conversion function,
00:05:04.261
which converts between the data formats,
00:05:07.548
and validates the data, before making that
00:05:11.383
conversion.
00:05:12.031
And by doing that, by having these
00:05:14.303
different types of data in different types,
00:05:16.575
we can't accidentally pass the data into
00:05:18.847
the wrong function.
00:05:19.921
It stops us accidentally passing untrusted input
00:05:23.853
data as an SQL string.
00:05:26.761
But that only works if we use
00:05:29.494
different types for different things. If we
00:05:32.228
use a string everywhere, then the programmer
00:05:34.961
must keep in their head “is this
00:05:37.695
trusted? Is this untrusted? Has it been
00:05:40.428
validated? Has it been correctly escaped?”
00:05:42.871
And it's easy to make mistakes.
00:05:45.781
If we use the same type everywhere,
00:05:48.555
if we use the same string type
00:05:51.328
to represent input data from the network
00:05:54.102
and student names, as we use to
00:05:56.875
represent the SQL commands, the compiler can't
00:05:59.649
help us.
00:06:00.541
So my first suggestion: to improve the
00:06:03.808
security of your systems, think about what
00:06:07.076
the different types are, and use different
00:06:10.343
types to represent different sorts of data.
00:06:13.711
Use a different type to represent the
00:06:17.320
parsed, validated, trusted, data, from that used
00:06:20.929
to represent the untrusted input data,
00:06:24.023
and carefully convert between them.
00:06:27.601
Make sure when you are converting data,
00:06:31.072
make sure that there are explicit type
00:06:34.543
conversion functions, and use these to enforce
00:06:38.014
the security boundaries.
00:06:39.601
Untrusted input data
00:06:42.931
is one thing, it needs to be
00:06:45.029
handled with care. But we can process
00:06:47.126
it, we can escape it, we can
00:06:49.224
try to convert it to some safe,
00:06:51.322
trusted, internal form,
00:06:52.321
making sure that all of meta characters,
00:06:54.821
the escape sequences, and so on have
00:06:57.322
been correctly handled. We can validate it
00:06:59.822
before we're using it.
00:07:01.351
And we can write explicit conversion functions,
00:07:03.809
to convert between the type representing the
00:07:06.268
input data, and the type representing the
00:07:08.726
data to be processed.
00:07:10.231
And this can ensure that only legal
00:07:12.706
conversions occur, and it can ensure that,
00:07:15.181
in order to convert the data,
00:07:17.303
it has to be validated.
00:07:19.171
And this combination, of using different types
00:07:22.958
for input data and parsed, processed,
00:07:26.205
validated, data, and explicit conversion functions,
00:07:29.551
and making sure the only way to
00:07:32.429
convert between those types is by using
00:07:35.308
a valid, validating, conversion function, makes sure
00:07:38.186
that only legal conversions can happen.
00:07:40.653
And helps make sure that we don't
00:07:43.531
accidentally pass untrusted data somewhere where we
00:07:46.410
shouldn’t.
00:07:46.921
It's a way of enforcing the security
00:07:49.686
boundaries, a way of distinguishing secure and
00:07:52.451
insecure data,
00:07:53.341
and of only allowing conversions between those
00:07:56.118
that validate the data integrity.
00:08:02.371
And we should think about ways of
00:08:05.799
labelling the data, ways of labelling whether
00:08:09.227
data has been checked or not,
00:08:12.165
and ways of tagging it with the
00:08:15.593
assumptions were making about its processing.
00:08:18.631
And it languages like Rust, or many
00:08:23.140
other more modern strongly-typed languages,
00:08:26.461
it's entirely possible to add tags to
00:08:30.910
the types.
00:08:32.281
In entirely possible in Rust, for example,
00:08:34.992
to define a struct that has no
00:08:37.704
fields.
00:08:38.191
And the result has no size,
00:08:40.378
because it has no content, but it
00:08:42.930
can be used as a parameter,
00:08:45.117
as a type parameter, to add a
00:08:47.668
semantic tags to the data.
00:08:49.591
So in the example we define two
00:08:53.746
structs, UserInput and Sanitised,
00:08:56.221
both of which are empty. They have
00:08:59.076
no size, no fields and therefore no
00:09:01.931
size, but we tag the Html type
00:09:04.785
with a
00:09:05.701
type parameter, which is either UserInput or
00:09:10.260
Sanitised.
00:09:11.011
And we can write a sanitise_html() function,
00:09:14.389
that takes HTML in the form of
00:09:17.766
user input, and returns sanitised HTML,
00:09:20.761
to convert between the two forms,
00:09:22.730
and we can make sure that everything
00:09:25.027
that takes HTML takes one of these
00:09:27.325
two variants.
00:09:28.081
So we can use it to represent
00:09:30.548
whether a particular processing step has been
00:09:33.015
performed. We can use it to represent
00:09:35.482
states in the various state machines that
00:09:37.949
define the behaviour in the system,
00:09:40.064
as we saw earlier in the course.
00:09:42.631
Again, the point is to label the
00:09:45.797
data, to specify what has been done
00:09:48.963
to the data, to make the assumptions
00:09:52.129
explicit.
00:09:52.681
And then the compiler can help us,
00:09:55.418
by checking that the data we're passing
00:09:58.156
around has been correctly tagged
00:10:00.211
as having had the particular sanitising operations
00:10:05.899
performed.
00:10:06.811
It doesn't stop the bugs,
00:10:09.961
it just limits the scope of them.
00:10:14.251
Like if we mess up, if we
00:10:16.775
write the conversions wrongly, the compiler can
00:10:19.298
detect the problems for us.
00:10:25.171
And, to be clear,
00:10:27.691
the use of memory safe languages,
00:10:30.181
the use of strong typing, the use
00:10:33.086
of
00:10:33.601
explicit types, different types for validated or
00:10:37.781
unvalidated data, conversion functions, semantic tags,
00:10:41.363
all of these things are not going
00:10:45.543
to eliminate security vulnerabilities.
00:10:48.031
The use of memory safe languages is
00:10:51.256
not going to eliminate security vulnerabilities.
00:10:54.121
What they do, though, is eliminate certain
00:11:00.687
classes of vulnerabilities.
00:11:03.601
When we're writing networked code,
00:11:06.181
security critical code in C,
00:11:09.361
we have to be continually on the
00:11:13.006
lookout for buffer overflows, for example.
00:11:16.231
We have to keep debugging code,
00:11:19.428
keep fixing buffer overflows, one at a
00:11:23.158
time.
00:11:23.791
If we write the code in Rust,
00:11:27.488
buffer overflows can't happen.
00:11:29.701
We've eliminated a particular class of vulnerability.
00:11:35.401
This is the benefit of memory safe
00:11:38.204
languages. This is the benefit of strongly-typed
00:11:41.007
languages. It's not that they get rid
00:11:43.810
of security vulnerabilities, it’s that they get
00:11:46.613
rid of particular types of security vulnerability,
00:11:49.417
and let us focus our efforts on
00:11:52.220
the other types.
00:11:53.521
The use of strong typing doesn't get
00:11:56.069
rid of security vulnerabilities, but it lets
00:11:58.617
us make our assumptions clear. It lets
00:12:01.165
us more clearly specify to the compiler
00:12:03.713
what our goal is in writing the
00:12:06.261
code, and the compiler can then help
00:12:08.809
us check that the code meets that
00:12:11.357
goal.
00:12:11.821
And, to the extent that vulnerabilities occur
00:12:19.141
because the attackers have ways of violating
00:12:21.977
those assumptions, spotting the cases we've missed,
00:12:24.813
the more we can specify, so the
00:12:27.649
compiler can help us spot those cases,
00:12:30.485
the less likely it is that these
00:12:33.321
vulnerabilities persist.
00:12:34.231
We're never going to get rid of
00:12:37.285
security vulnerabilities entirely. There's no magic silver
00:12:40.339
bullet here.
00:12:41.311
But we can eliminate certain types of
00:12:44.568
vulnerability. And we can make vulnerabilities in
00:12:47.824
the code which does exist less likely
00:12:51.181
by clearly specifying the assumptions.
00:13:00.841
And, as programmers, we need to start
00:13:03.121
thinking about how we write code.
00:13:11.011
If you look up, for example,
00:13:14.115
the ACM code of ethics,
00:13:16.801
one of the key
00:13:19.441
things every engineer has to do,
00:13:23.287
is avoid causing harm.
00:13:25.951
When we're building systems, whether they're software
00:13:29.950
systems, or bridges, or any other type
00:13:33.948
of engineering,
00:13:35.191
we have to avoid causing harm.
00:13:39.601
And for the Civil Engineers, that means
00:13:42.044
using appropriate techniques to build the bridges
00:13:44.487
such that they don't fall down.
00:13:46.681
For the software engineers, it means using
00:13:50.272
appropriate techniques to make sure our software
00:13:53.862
doesn't cause harm.
00:13:55.501
And, unfortunately, security vulnerabilities, and software failures,
00:14:02.176
do routinely cause harm.
00:14:06.091
And I think the question I want
00:14:09.381
you to think about, is whether you
00:14:12.671
are following best practices to avoid that
00:14:15.961
harm.
00:14:16.531
If you're writing a networked system in
00:14:19.725
C,
00:14:20.281
can you actually claim you're following best
00:14:24.456
practices to avoid harm,
00:14:26.941
when we know that a memory unsafe
00:14:30.135
language
00:14:30.691
is likely to suffer from buffer overflows,
00:14:34.635
use after free bugs, race conditions,
00:14:38.016
and so on, which cause security vulnerabilities.
00:14:42.061
Can you justify your professional practice?
00:14:47.491
And I think, we are rapidly getting
00:14:51.780
to the stage where,
00:14:54.331
some sort of software failure or security
00:14:57.724
vulnerability is going to cause significant harm.
00:15:01.118
And that is going to result in
00:15:04.511
a lawsuit.
00:15:05.581
And some software engineer, somewhere, is going
00:15:08.853
to be up in court, and will
00:15:12.126
be asked to justify their processes,
00:15:15.031
and will be asked to justify did
00:15:18.633
you follow best practices?
00:15:20.791
And we all know that people are
00:15:23.264
not perfect, we all know that mistakes
00:15:25.738
happen.
00:15:26.191
And the issue is not making sure
00:15:29.850
that no-one ever makes a mistake.
00:15:32.986
The issue is making sure people follow
00:15:36.644
best practices, to mitigate human errors,
00:15:39.780
and to mitigate risk.
00:15:41.971
And I would argue that the best
00:15:45.160
practice today
00:15:46.171
is using memory safe languages, is using
00:15:51.254
strong type systems, is using structured methodologies
00:15:56.337
for defining our software.
00:15:59.341
And it's not clear that we're necessarily
00:16:01.917
always doing that.
00:16:03.121
But as a community, I think we
00:16:04.177
want to shift towards building software in
00:16:05.234
a in a more structured, more well-defined,
00:16:06.290
more secure, way, before we are forced
00:16:07.346
to, by threat of lawsuit.
00:16:20.731
And that's what I want to say
00:16:23.173
about security.
00:16:23.971
I’ve spoken about memory safety, I’ve talked
00:16:26.973
about how
00:16:27.931
memory unsafe languages have a large number
00:16:31.435
of undefined behaviours, which tend to lead
00:16:34.939
to exploitable vulnerabilities.
00:16:36.541
I spoke a little bit about parsing,
00:16:40.461
the problems of parsing unstructured data,
00:16:43.921
and suggested we need
00:16:47.131
formalised parsing libraries, formal definitions of the
00:16:50.145
input grammar, such that we can be
00:16:53.159
sure what is accepted, and what is
00:16:56.173
not, by our systems, and to reduce
00:16:59.187
some of the risks in parsing untrusted
00:17:02.200
data.
00:17:02.731
And I said a little bit about
00:17:06.422
how by carefully structuring our code,
00:17:09.587
and documenting the assumptions using modern type
00:17:13.278
systems, we can reduce the risks that
00:17:16.970
the system has inconsistencies and unexplainable behaviours.
00:17:20.761
As professional engineers, we need to build
00:17:23.920
systems which are secure and fit for
00:17:27.080
purpose.
00:17:27.631
By gradually moving to
00:17:32.881
memory safe languages, to languages which have
00:17:36.074
stronger type systems, that allow us to
00:17:39.267
formally specify their behaviours,
00:17:41.191
and that help us by checking those
00:17:43.991
behaviours, I think we can start to
00:17:46.791
avoid some of the problems inherent
00:17:49.291
in a lot of code. And I
00:17:51.706
think as professional, ethical, engineers it behooves
00:17:54.121
us to do that.
Discussion
Lecture 9 discussed some security considerations when writing system programs.
It noted the risks of memory unsafety in programming languages, and how this
can lead to undefined behaviour.
And it highlighted some of the types of undefined behaviour that are
possible in C and C++ programs.
It outlined how such undefined behaviour can lead to security vulnerabilities,
and presented statistics showing that more than half of all security problems
in software are due to memory safety issues.
It highlighted some of the tools that are available to help mitigate these
problems in C and C++, and suggested a gradual rewrite of memory unsafe
systems into safe languages.
Part 2 noted that parsing network data is a frequent source of
vulnerabilities, and suggested that parsers can be improved. It
noted that ad-hoc, hand-written, parsing code is commonly not safe or
robust, and suggested the use of formal grammars and parser generator
systems to produce robust input parsers. It noted the limitations of
Postel's law for modern systems, and suggested that robust protocol
implementations, when the protocol is seeing broad deployment, need
to have both a well-defined input grammar but also well-defined failure
modes, if they are to be consistent, robust, and secure.
Finally, part 3 noted that security vulnerabilities are often caused
by adversaries finding a way to exploit inconsistencies and mistaken
assumptions about the way data is processed in a system. It noted that,
while there is no silver bullet, strongly typed languages can help a
programmer to express the constraints and assumptions inherent in a
design, helping the compiler to check the code for consistently. This
can help reduce the risk of security vulnerabilities. The benefits of
strongly typed languages in avoiding common bugs were also noted.
The lecture concluded by discussing the ACM code of ethics, and the
requirement for professional engineers to build systems that do no
harm. It suggested that memory unsafe, weakly typed, systems can no
longer be regarded as best current practice for building many types
of system.
Discussion will focus on the types of undefined behaviour present in
C programs and how they lead to security vulnerabilities. On the
limitations of Postel's law and on the appropriate way to parse data
in networked systems. And on the supposed benefits of strongly typed
languages for security.