CodeBlanks Logo Icon CodeBlanks

Security layer

Write buffer overflow protection and security checks.

Security layer

In the fourth lesson you add the security layer. The security_check function checks a buffer for overflow risk: if the length is too large or the pointer is NULL, it returns 0.

Buffer overflow is exceeding an array's bounds; it is the most common source of security vulnerabilities. security_check verifies the size before processing data. len < 0 || len > 16 catches negative or too-large values; buf == NULL catches a null pointer. If both pass, it returns 1 (safe). This is a basic example of defensive programming.

Defensive programming

Defensive programming catches potential errors in advance. security_check does three checks: is the length negative, too large, or is the pointer NULL. If any check fails, it returns 0.

This approach is similar to a canary: an error is caught before it occurs. A real canary is placed next to a function's return address to detect stack smashing; if it changes, the program stops. security_check checks before data entry. Both methods prevent bad data from entering the system. Secure code checks every input.

Practice Add the security_check function

Below the existing definitions, write int security_check(const char *buf, int len). Check overflow with if (len < 0 || len > 16) return 0;. Check NULL with if (buf == NULL) return 0;. Return safe with return 1;.

Starter code

main.c
#include <stdio.h>
#include <string.h>

void kernel_init(void);
int parse_command(const char *cmd);
int security_check(const char *buf, int len);

void kernel_init(void) {
    printf("Çekirdek başladı\n");
}

int syscall(int num) {
    return num * 2;
}

int parse_command(const char *cmd) {
    if (strcmp(cmd, "help") == 0) return 1;
    if (strcmp(cmd, "exit") == 0) return 2;
    return 0;
}

int main(void) {
    printf("Proje başladı\n");
    kernel_init();
    printf("Komut: %d\n", parse_command("help"));
    return 0;
}

// security_check fonksiyonunu yaz:
// len 0-16 arası ve buf NULL değilse 1, yoksa 0

Sample solution

main.c
#include <stdio.h>
#include <string.h>

void kernel_init(void);
int parse_command(const char *cmd);
int security_check(const char *buf, int len);

void kernel_init(void) {
    printf("Çekirdek başladı\n");
}

int syscall(int num) {
    return num * 2;
}

int parse_command(const char *cmd) {
    if (strcmp(cmd, "help") == 0) return 1;
    if (strcmp(cmd, "exit") == 0) return 2;
    return 0;
}

int security_check(const char *buf, int len) {
    if (len < 0 || len > 16) return 0;
    if (buf == NULL) return 0;
    return 1;
}

int main(void) {
    printf("Proje başladı\n");
    kernel_init();
    printf("Komut: %d\n", parse_command("help"));
    return 0;
}

Expected output

Proje başladı
Çekirdek başladı
Komut: 1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does security_check prevent?

  • Type errors
  • Buffer overflow Correct answer
  • Compile errors
  • Memory leaks

Explanation: security_check checks for buffer overflow risk.

2 Complete the length check

Question
if (len < 0 || len > ____) return 0;
Answer
if (len < 0 || len > 16) return 0;

Explanation: len > 16 catches overflow risk; 16 bytes is the limit.

3 Match security concept

Term Meaning
Overflow check len > 16
NULL check buf == NULL
Canary Protection value
Safe return 1

All lessons in Expert