Security

On this page
  1. The vocabulary
  2. Authorisation
  3. Authentication and passwords
  4. Encryption
  5. Security in practice
  6. Buffer overflows

Security theory – authorisation, authentication, encryption – and practice: real attacks such as the Morris worm and the Thompson compiler backdoor, and how buffer overflows on the stack and heap work.

The vocabulary

Principals
Users, programs, system administrators…
Authorisation
Who is permitted to do what?
Authentication
How do we know who the user is?
Encryption
Privacy and authentication across an insecure network.
Auditing
A record of who changed what, for diagnosis after the fact.

Authorisation

An access control matrix lists, for every protected resource, who is permitted to do what. For example, each UNIX file or directory has read, write and execute permissions for owner, group and world. A smartphone keeps a list of permissions granted to each app.

Setuid: a program that runs with the permissions of the principal who installed it, not the user who runs it.

Principle of least privilege

Grant each principal the least permission possible for its job:

  • minimise code running inside the kernel,
  • minimise code running as system administrator,
  • give users exactly the permissions they need,
  • keep the number of setuid-root programs to an absolute minimum.

Running a web server as root breaks the principle. In practice it's hard to know in advance which permissions an app or server will need.

The trusted computing base is the set of software trusted to enforce the security policy. Servers often have to be trusted (a storage server stores and retrieves data whoever asks), so a flaw in the server gives the attacker control.

Authentication and passwords

Short passwords are easy to remember and easy to guess; long ones are hard to remember. Where should they be stored?

  • In a file: anyone with admin rights can read it.
  • Encrypted (hashed) in a file: if attackers get the file, they can check guesses offline, and reused passwords can be checked against other systems.
  • Hashed with a random salt: the password and a random salt are hashed together. This defeats precomputed password tables (rainbow tables).

Two-factor authentication: store the private key on a smartcard chip, authenticate the card with challenge–response, and use a PIN to prove the user has the card.

Encryption

The sender encrypts plaintext M with a function E and key KE into ciphertext C; the receiver decrypts with D and KD. Even if E, D, M and C are known, it should be impossible to find the keys or modify the message. Encryption is the basis for authentication, privacy and integrity.

Symmetric key (DES, IDEA)
One key shared by both parties and kept secret from everyone else. Ciphertext = (M)K.
Public key (RSA, PGP)
Keys come in pairs. The public key can be published; the private key is secret. You cannot derive the private key from the public one.
GoalEncrypt withDecrypt with
Secrecy: only the receiver can read itReceiver's public keyReceiver's private key
Authentication: only the sender could have sent itSender's private keySender's public key

So two parties who want to talk secretly with asymmetric encryption exchange public keys; private keys are never shared.

Session keys

Public key encryption is slow, so it's used to set up a shared session key for the rest of the conversation. With symmetric keys alone, everyone can't share a secret with everyone else, so an authentication server (Kerberos) shares a separate secret with each user and hands out session keys for A ↔ B. Everyone must trust that server: if it is compromised, the attacker can do anything.

Message digests (MD5, SHA)

A cryptographic checksum for message integrity. Small compared to the message (MD5 is 128 bits) and one-way: it's infeasible to find two messages with the same digest. The message is processed in 512-bit blocks, each transforming the running digest.

Security in practice

Real systems are not that secure. Attackers go after the weakest link: any system with bugs is vulnerable, and it is usually not a brute-force attack on the encryption. It's often impossible to tell whether a system is compromised, and hard to re-secure it, because attackers can leave backdoors.

Tenex password check
The kernel compared the password one character at a time and returned at the first mismatch. On this early virtual-memory system, that let attackers find the password one character at a time.
Morris worm (1988)
Spread with a password dictionary, a sendmail bug (debug access enabled by default, known for years but not fixed), and fingerd, which copied a string into a fixed-size stack buffer without checking its length. The worm encoded itself into that string.
Ping of death
A malformed IP fragment with offset + length larger than the maximum packet size; the kernel's reassembly code didn't check.
UNIX talk
Setuid root (to write to every terminal), with a bug in its Ctrl-C signal handler.
Netscape
Seeded its random number generator with the time of day, making session keys predictable. The patch was offered for download over the web – a four-byte change could make it use an attacker's key.
Code Red, Nimda, Slammer
Worms using known vulnerabilities. Slammer was a single UDP packet and infected 100K+ machines in under 10 minutes. Million-node botnets are now common.

The Thompson compiler backdoor

  1. Change login.c: if the name is “ken”, skip the password check and log in as root. Too obvious.
  2. Change the C compiler: when it sees a trigger, insert the backdoor into login.c. Now login.c only contains the trigger.
  3. Change the compiler again: when it sees a second trigger (compiling the compiler), insert both changes. Compile the compiler, then remove the change from the compiler's source.

Every compiler built with an infected compiler is infected, even cross-compiled for new machines, and no source code shows it. Lesson: it is hard to re-secure a machine after a break-in, or even to detect one.

Finding and disclosing bugs

  • Responsible disclosure: tell the developers first, work on a fix together, go public if there's no response.
  • Bug bounty programs.
  • Selling zero-days to brokers who resell them – ethically questionable.

Buffer overflows

A buffer overflow happens when a program writes more data into a buffer than it has room for. The classic culprit is gets(), which has no idea how big the buffer is:

#include <stdio.h>
int main()
{
    int a = 0;
    char buf[10];
    int b = 0;
    gets(buf);
    printf("a=%d, b=%d\n", a, b);
}

Typing more than 10 characters overwrites the neighbouring variables on the stack, so a or b is no longer 0.

On the stack
Local variables can be corrupted, and the saved return address can be overwritten. The attacker then controls where the function returns – control of program execution.
On the heap
Allocated data after the buffer can be corrupted. There is no return address on the heap, but function pointers stored there can be overwritten, which also gives control of execution.

Mitigations

  • Use safe library calls: fgets(buf, sizeof(buf), stdin) instead of gets.
  • Code review.
  • Compiler protections: -fstack-protector (stack canaries), -pie (position-independent executables).
  • ASLR: randomising the stack, heap and text segment makes valid addresses hard to find.
  • Run vulnerable programs with reduced permissions, or in a container or VM.

On earlier exams

Practise this topic in the quiz (15 questions)