SQL Injection
SQL injection is building a SQL string with untrusted text so the attacker can change the statement. In C the usual hole is sprintf(sql, "SELECT * FROM t WHERE name='%s'", user). The fix is a bound parameter.
Detailed Explanation
SQL is a language. If the user gets to write part of the source, they get to write the program. user = "x' OR '1'='1" turns a lookup into a tautology. user = "x'; DROP TABLE t; --" tries to run a second statement. sqlite3_exec will run multiple statements in one string. That is convenient and is why exec plus concatenation is the worst pairing.
Escaping quotes by hand (' → '') is incomplete. Encodings, LIKE metacharacters, and identifiers ("table" from user input) have different rules. Identifiers cannot be bound as parameters; you must allow-list them (order is ASC or DESC, column is one of a known set). Values can always be bound.
The C API makes the right path slightly longer, which is why people skip it. Prepare WHERE name = ?, bind_text, step. The extra lines are the security boundary. A helper that takes a format string and a user value and concatenates them is not a helper; it is a vulnerability.
Injection is not only HTTP. A line from a file, a argv flag, a row copied from another database, a CGI QUERY_STRING after a sloppy decode — if it is not a literal you typed, it is data.
Defense in depth: the database user (when you have one) gets the least privilege. SQLite is in-process, so "least privilege" is your OS user and the file mode on the .db. Bound parameters still matter.
Code Examples
#include <stdio.h>
#include <sqlite3.h>
/* Safe: user is data. */
static int find_name(sqlite3 *db, const char *user)
{
sqlite3_stmt *st = NULL;
if (sqlite3_prepare_v2(db, "SELECT id FROM t WHERE name = ?", -1, &st, NULL)
!= SQLITE_OK) {
return -1;
}
sqlite3_bind_text(st, 1, user, -1, SQLITE_TRANSIENT);
int rc = sqlite3_step(st);
sqlite3_finalize(st);
return rc == SQLITE_ROW ? 1 : 0;
}
int main(void)
{
sqlite3 *db = NULL;
sqlite3_open(":memory:", &db);
sqlite3_exec(db, "CREATE TABLE t(id INTEGER, name TEXT);", NULL, NULL, NULL);
printf("%d\n", find_name(db, "x' OR '1'='1")); /* prints: 0 */
sqlite3_close(db);
return 0;
}#include <stdio.h>
#include <string.h>
/* Unsafe: do not do this. Shown as the contrast. */
static void bad(char *sql, size_t n, const char *user)
{
snprintf(sql, n, "SELECT id FROM t WHERE name='%s'", user);
}
int main(void)
{
char sql[80];
bad(sql, sizeof sql, "x' OR '1'='1");
printf("%s\n", sql);
/* prints a tautology, not a lookup of that name */
return 0;
}Common Gotchas
`sqlite3_exec` plus string concatenation can run a second statement
`DROP TABLE` is a real payload.
Quoting by replacing `'` is not a complete defense
bind values; allow-list identifiers.
Integers from `atoi` of user text should still be bound
the habit stays, and overflow is a separate bug.
A CGI or HTTP query string is attacker-controlled even after URL-decode
A CGI or HTTP query string is attacker-controlled even after URL-decode