The provided code implements a program in C that allows...
The provided code implements a program in C that allows the user to manipulate a database of three "Utente" (user) records. Each Utente
has an ID, a number of credits (crediti
), and a bonus level (livelloBonus
). However, the program includes vulnerabilities stemming from insufficient input validation that can lead to unintended modifications to internal data structures. Here's what each part of the code does:
Breakdown of Functionality:
-
Utente
struct definition and initialization:- The
Utente
struct contains three fields:id[10]
(a string representing the user ID).crediti
(an integer representing the user's credits).livelloBonus
(an integer representing a bonus level or flag).
- An array
utenti
is pre-initialized with three users, each with a unique ID, a certain number of credits, and alivelloBonus
field set to0
.Utente utenti[3] = { {"U001",100,0}, {"U002",50,0}, {"U003",75,0} };
- The
-
aggiungiCreditiAvanzato
function: This function allows a user to add credits to an existingUtente
by providing the user ID and the number of credits to add. However, it is poorly implemented and vulnerable due to lack of robust input validation.-
The user is prompted to input an
id
(user ID) and a value (credits to add):printf("Inserisci ID utente: "); scanf("%9s", id);
-
The program searches for the corresponding
Utente
in theutenti
array:for(int i=0; i<3; i++) { if(strcmp(utenti[i].id, id) == 0) { u = &utenti[i]; break; } }
-
If the user is not found (
u == NULL
), the function exits with an error message. -
If the user is found, the program prompts the user to input the credits to add. However, the input is stored as a string (
input[50]
) for later processing:printf("Inserisci crediti da aggiungere: "); scanf(" %49[^\n]", input);
Inclusion of Vulnerability:
The input string
input
is parsed in two ways:-
If the input is only a valid integer (e.g., "10"), it correctly adds the value to the user's credits using:
if(sscanf(input, "%d", &aggiunta) == 1) { u->crediti += aggiunta; }
-
If the input contains other characters (e.g., "10+LVL"), it enters a custom parsing loop:
char* ptr = input; while(*ptr) { if(*ptr >= '0' && *ptr <= '9') { aggiunta += atoi(ptr); while(*ptr >= '0' && *ptr <= '9') ptr++; } else if(*ptr == 'L' || *ptr == 'V') { u->livelloBonus += 1; ptr++; } else ptr++; }
In this loop:
- Numbers are extracted and added to
aggiunta
(which increases the user's credits). - Any occurrence of the characters
L
orV
increaseslivelloBonus
by1
.
- Numbers are extracted and added to
This behavior is a security vulnerability because malformed or malicious input can manipulate both
crediti
andlivelloBonus
in unexpected ways. -
-
Summary Output: After processing, the function prints the updated user information:
printf("Utente %s: crediti=%d, livelloBonus=%d\n", u->id, u->crediti, u->livelloBonus);
-
main
Function: The program simply callsaggiungiCreditiAvanzato
when run. Only one input session occurs per execution.
Vulnerable Behavior:
The function is vulnerable due to the custom parsing logic for input
. For example:
- A normal input like
"10"
works as intended and adds10
credits. - A malicious input like
"10LVL"
would:- Add
10
credits to thecrediti
field. - Increase the
livelloBonus
field by2
(once for eachL
andV
).
- Add
This input parsing logic can lead to unintended side effects, making the program vulnerable to malicious input.
Example Input and Output:
-
Normal Input:
Inserisci ID utente: U001 Inserisci crediti da aggiungere: 20 Utente U001: crediti=120, livelloBonus=0
-
Malicious Input:
Inserisci ID utente: U001 Inserisci crediti da aggiungere: 10LVL Utente U001: crediti=110, livelloBonus=2
Key Issues:
- Lack of input validation allows arbitrary manipulation of both
crediti
andlivelloBonus
. - Mixing numeric parsing and handling of special characters opens doors to unintended or maliciously crafted inputs.
- The program assumes inputs are well-formed and does not handle unexpected formats securely.
Closing Thoughts:
This code demonstrates how improper handling of user input can lead to vulnerabilities. Fixed behaviors such as validating input strictly (e.g., ensuring it's strictly numeric before parsing) and not allowing unintended side effects are essential for secure programming.