graph TD
A[Input Received] --> B[Validate Length]
B --> C[Sanitize Content]
C --> D[Type Checking]
D --> E[Boundary Validation]
E --> F[Safe Processing]
F --> G[Memory Management]
Technique |
Description |
Security Impact |
Input Validation |
Check input against predefined rules |
Prevent malicious inputs |
Sanitization |
Remove/escape dangerous characters |
Reduce injection risks |
Type Enforcement |
Ensure input matches expected type |
Prevent type-related vulnerabilities |
Memory Protection |
Manage buffer boundaries |
Prevent buffer overflows |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_INPUT_LENGTH 100
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
} User;
int sanitize_input(char *input) {
// Remove non-alphanumeric characters
size_t j = 0;
for (size_t i = 0; input[i] != '\0'; i++) {
if (isalnum(input[i]) || input[i] == ' ') {
input[j++] = input[i];
}
}
input[j] = '\0';
return j;
}
User* create_user() {
User *new_user = malloc(sizeof(User));
if (!new_user) {
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
// Safe name input
char name_buffer[MAX_INPUT_LENGTH];
printf("Enter name: ");
if (fgets(name_buffer, sizeof(name_buffer), stdin) == NULL) {
free(new_user);
return NULL;
}
// Remove newline
name_buffer[strcspn(name_buffer, "\n")] = 0;
// Sanitize and validate name
if (sanitize_input(name_buffer) == 0 ||
strlen(name_buffer) >= MAX_NAME_LENGTH) {
free(new_user);
return NULL;
}
// Safe name copying
strncpy(new_user->name, name_buffer, MAX_NAME_LENGTH - 1);
new_user->name[MAX_NAME_LENGTH - 1] = '\0';
// Safe age input
printf("Enter age: ");
if (scanf("%d", &new_user->age) != 1 ||
new_user->age < 0 || new_user->age > 120) {
free(new_user);
return NULL;
}
// Clear input buffer
while (getchar() != '\n');
return new_user;
}
int main() {
User *user = create_user();
if (user) {
printf("User created: %s, Age: %d\n", user->name, user->age);
free(user);
} else {
printf("User creation failed\n");
}
return 0;
}
-
Comprehensive Validation
- Check input length
- Validate input type
- Enforce content rules
-
Sanitization Techniques
- Remove special characters
- Escape potential threat characters
- Normalize input format
LabEx Security Recommendations
At LabEx, we emphasize:
- Implement multi-layer input validation
- Use context-specific sanitization
- Employ defensive programming techniques
Advanced Protection Mechanisms
graph LR
A[Input] --> B{Length Check}
B --> C{Sanitization}
C --> D{Type Validation}
D --> E{Boundary Check}
E --> F[Safe Processing]
Memory Safety Considerations
- Always allocate memory dynamically
- Use
strncpy()
instead of strcpy()
- Implement strict boundary checks
- Free allocated memory immediately after use
Error Handling Best Practices
- Provide clear error messages
- Log security-related events
- Implement graceful failure mechanisms
- Never expose system details in error outputs
By adopting these secure input handling techniques, developers can create robust and resilient C programs that effectively mitigate potential security risks.