Files
Claude 3d748c40d2
deploy / deploy (push) Successful in 1m3s
Console ↔ messagerie sans reconnexion, skills de sécurité et audit
Passage d'un site à l'autre (admin/src/handoff.ts) :
- boutons « Messagerie » (console) et « Console » (messagerie) ;
- session transmise par postMessage à l'onglet ouvert, après vérification
  de l'adresse exacte, de la fenêtre et d'un numéro à usage unique ;
  rien dans l'adresse ni dans le stockage, le serveur revérifie tout ;
- adresses de test (VITE_ADMIN_ORIGIN / VITE_MAIL_ORIGIN) refusées en CI.

Skills ajoutés (.claude/skills) : sharp-edges et differential-review de
Trail of Bits (CC BY-SA 4.0, texte seul, relus avant ajout).

Audit de sécurité, corrections :
- admin.may_enter : un admin suspendu ou sans TOTP n'entre plus dans la
  console ni la messagerie (avant : seule la liste des admins comptait) ;
- refus d'accès journalisés / signalés une fois par compte toutes les
  10 min : plus de journal rempli ni de boîte d'alertes noyée ;
- e-mails affichés avec une CSP propre au cadre : aucune ressource
  extérieure avant « Afficher les images » (SVG, CSS échappé, @import) ;
- pièces jointes toujours téléchargées (type forcé), jamais ouvertes
  comme une page de mail.bobinoo.org ;
- envoi : en-têtes sans caractères de contrôle, uid « répondu » validé ;
- lectures relancées une fois sur 502/503/504 du relais (jamais les
  actions) ;
- npm audit fix (cross-spawn, outil de build).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-12-01 11:22:05 +00:00

5.5 KiB

C/C++ Sharp Edges

Integer Overflow is Undefined Behavior

// DANGEROUS: Signed overflow is UB, compiler can optimize away checks
int x = INT_MAX;
if (x + 1 > x) {  // Compiler may assume always true (UB)
    // Overflow check optimized away!
}

// DANGEROUS: Size calculations
size_t size = user_count * sizeof(struct User);
// If user_count * sizeof overflows, allocates tiny buffer
void *buf = malloc(size);

The Problem: Signed integer overflow is undefined behavior. Compilers assume it never happens and optimize accordingly—including removing overflow checks.

Detection: Look for arithmetic on signed integers, especially in size calculations, loop bounds, and allocation sizes.

Buffer Handling

// DANGEROUS: No bounds checking
char buf[64];
strcpy(buf, user_input);       // Classic overflow
sprintf(buf, "Hello %s", name); // Format + overflow
gets(buf);                      // Never use, removed in C11

// DANGEROUS: Off-by-one
char buf[64];
strncpy(buf, src, 64);         // NOT null-terminated if src >= 64!
buf[63] = '\0';                // Must do manually

// DANGEROUS: snprintf return value
int ret = snprintf(buf, sizeof(buf), "%s", long_string);
// ret is length that WOULD be written, not actual length
// If ret >= sizeof(buf), output was truncated

Safe Alternatives:

  • strlcpy, strlcat (BSD, not standard)
  • snprintf with proper return value checking
  • C11 Annex K strcpy_s, sprintf_s (limited support)

Format Strings

// DANGEROUS: User controls format
printf(user_input);            // Format string attack
syslog(LOG_INFO, user_input);  // Same problem
fprintf(stderr, user_input);   // Same problem

// Attacker input: "%x%x%x%x" → leaks stack
// Attacker input: "%n" → writes to memory

// SAFE: Format as literal
printf("%s", user_input);

Detection: Any *printf family function where the format argument is not a string literal.

Memory Cleanup

// DANGEROUS: Compiler may optimize away
char password[64];
// ... use password ...
memset(password, 0, sizeof(password));  // May be removed!

// The compiler sees: "writes to password, then password goes out of scope"
// Optimization: "dead store elimination" removes the memset

Safe Alternatives:

// Option 1: explicit_bzero (BSD, glibc 2.25+)
explicit_bzero(password, sizeof(password));

// Option 2: SecureZeroMemory (Windows)
SecureZeroMemory(password, sizeof(password));

// Option 3: Volatile function pointer trick
static void *(*const volatile memset_ptr)(void *, int, size_t) = memset;
memset_ptr(password, 0, sizeof(password));

// Option 4: C11 memset_s (limited support)
memset_s(password, sizeof(password), 0, sizeof(password));

Uninitialized Variables

// DANGEROUS: Uninitialized stack variables
int result;
if (condition) {
    result = compute();
}
return result;  // Uninitialized if !condition

// DANGEROUS: Uninitialized struct padding
struct {
    char a;      // 1 byte
    // 3 bytes padding (uninitialized)
    int b;       // 4 bytes
} s;
s.a = 'x';
s.b = 42;
send(sock, &s, sizeof(s), 0);  // Leaks 3 bytes of stack

Fix: Use = {0} initialization or memset.

Double Free and Use-After-Free

// DANGEROUS: Double free
free(ptr);
// ... later ...
free(ptr);  // Heap corruption

// DANGEROUS: Use after free
free(ptr);
ptr->value = 42;  // Writing to freed memory

// DANGEROUS: Returning pointer to local
char *get_greeting() {
    char buf[64] = "hello";
    return buf;  // Stack pointer invalid after return
}

Mitigations:

  • Set pointer to NULL after free: free(ptr); ptr = NULL;
  • Use static analysis (Coverity, cppcheck)
  • Use AddressSanitizer in testing

Signal Handler Issues

// DANGEROUS: Non-async-signal-safe functions in handler
void handler(int sig) {
    printf("Got signal\n");  // NOT async-signal-safe
    malloc(100);             // NOT async-signal-safe
    free(ptr);               // NOT async-signal-safe
}

// Async-signal-safe: write(), _exit(), signal()
// Most functions including printf, malloc, free are NOT safe

Time-of-Check to Time-of-Use (TOCTOU)

// DANGEROUS: File state can change between check and use
if (access(filename, W_OK) == 0) {
    // Attacker replaces file with symlink here
    fd = open(filename, O_WRONLY);  // Opens different file
}

Fix: Open first, then check permissions on the file descriptor.

Variadic Function Pitfalls

// DANGEROUS: Wrong format specifier
printf("%d", (long long)value);  // %d expects int, not long long
printf("%s", 42);                // Interprets 42 as pointer

// DANGEROUS: Missing sentinel
execl("/bin/ls", "ls", "-l", NULL);  // NULL required!
execl("/bin/ls", "ls", "-l");        // Missing NULL = UB

Macro Pitfalls

// DANGEROUS: Macro arguments evaluated multiple times
#define SQUARE(x) ((x) * (x))
int a = 5;
SQUARE(a++);  // Expands to ((a++) * (a++)) - increments twice!

// DANGEROUS: Operator precedence
#define ADD(a, b) a + b
int x = ADD(1, 2) * 3;  // Expands to 1 + 2 * 3 = 7, not 9

// SAFER: Fully parenthesize
#define ADD(a, b) ((a) + (b))

Detection Patterns

Search for these patterns in C/C++ code:

Pattern Risk
strcpy, strcat, gets, sprintf Buffer overflow
printf(var) where var is not literal Format string
memset before variable goes out of scope Dead store elimination
free(ptr) without ptr = NULL Double free risk
malloc without overflow check on size Integer overflow
Arithmetic on int near INT_MAX Signed overflow UB
strncpy without explicit null termination Missing terminator