What Is a Secure Program
A program is secure if it does what it should, only what it should, and resists misuse by a hostile user. Every program is a collection of assumptions (about its input, its environment and its users), and most vulnerabilities come from an assumption that an attacker can break. Quality and security are related: poor, untested code is more likely to contain exploitable flaws. The aim is secure by design: security considered during requirements, design, coding and testing, not added after release.
Non-Malicious Program Errors
These are mistakes by programmers, not deliberate attacks, yet they give attackers an opening.
- Buffer overflow: data is written beyond the end of a fixed-size area of memory (buffer), overwriting neighbouring data or the return address, so an attacker can crash the program or run his own code. It is common in C and C++ with functions such as gets() and strcpy().
- Integer overflow: an arithmetic result is too large for its type and wraps around, causing wrong sizes or checks.
- Incomplete mediation: the program does not check every access or input, for example trusting values from a web form or a URL, which lets users change a price or an identifier.
- Time-of-check to time-of-use (TOCTOU) race: the state of a resource changes between the check and the use, such as a file that is replaced after its permission was checked.
- Unvalidated input and injection: input used in a command or database query without checking it, such as SQL injection, command injection and cross-site scripting.
- Format string errors, use-after-free and memory leaks in unmanaged languages.
- Poor error handling and information leakage: error messages that reveal paths, versions or queries.
- Hard-coded secrets and weak randomness.
Preventing These Errors
Validate all input (accept only known-good values), use bounded functions (strncpy, fgets, or safe languages such as Java and Python that check bounds), use parameterised queries, check results and errors, apply least privilege, and avoid revealing details in error messages.
Definition: Buffer overflow. A programming error in which more data is written into a fixed-size memory buffer than it can hold, overwriting adjacent memory and possibly letting an attacker alter the behaviour of the program.
Table 2.1: Non-malicious program errors
| Error | Cause | Prevention |
|---|---|---|
| Buffer overflow | Writing past a buffer | Bounds checks, safe functions |
| Incomplete mediation | Input or access not checked | Validate every input on the server |
| TOCTOU race | State changes between check and use | Atomic operations, locks |
| SQL injection | Input joined into a query | Parameterised queries |
Example 2.1: SQL injection and its prevention
Problem: A login page builds the query by joining the user name typed by the user into the text of the query. The table users has two rows (alice and bob). What does the query return when the user types the name x' OR '1'='1 and what happens with a parameterised query?
Solution:
- The joined query becomes: SELECT name FROM users WHERE name = 'x' OR '1'='1'.
- The condition '1'='1' is always true, so the query returns every row: alice and bob.
- With a parameterised query the typed text is treated purely as a value to compare with name.
- No user is called x' OR '1'='1, so zero rows are returned.
Result: The unsafe query returns 2 rows (the attacker is let in); the parameterised query returns none.
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE users (name TEXT, pwd TEXT)")
db.executemany("INSERT INTO users VALUES (?, ?)",
[("alice", "a1"), ("bob", "b2")])
name = "x' OR '1'='1"
q = "SELECT name FROM users WHERE name = '" + name + "'"
print(q)
print("Unsafe:", db.execute(q).fetchall())
safe = "SELECT name FROM users WHERE name = ?"
print("Safe:", db.execute(safe, (name,)).fetchall())
Output
SELECT name FROM users WHERE name = 'x' OR '1'='1'
Unsafe: [('alice',), ('bob',)]
Safe: []
Common mistakes
- Trusting data that comes from the client, such as hidden form fields.
- Using gets() or unbounded string copies in C programs.
- Building queries by joining strings.
- Showing detailed error messages to users.
Key takeaways
- A secure program does what it should and nothing more.
- Errors: buffer overflow, integer overflow, incomplete mediation, TOCTOU, injection.
- Validate all input; use bounded functions and parameterised queries.
- Build security in from design, not afterwards.
Practice questions
- What is a buffer overflow?
- What is incomplete mediation?
- How is SQL injection prevented?
Answers to the practice questions
- Writing more data into a buffer than it can hold, overwriting neighbouring memory.
- Failing to check every access or input, so that users can alter values.
- By parameterised queries and input validation.
Sources
- Official BCA syllabus Computer Security, course code 25BCA406DS02, Unit II
