matcha icon indicating copy to clipboard operation
matcha copied to clipboard

BUG: Potential integer overflow in POP3 offset/limit handling on 32-bit systems

Open andrinoff opened this issue 7 months ago • 2 comments

Describe the bug In backend/pop3/pop3.go lines 107-112, uint32 offset/limit values are cast to int without overflow checking. On 32-bit systems, if offset > 2^31-1, int(offset) becomes negative, causing array index bugs.

To reproduce

  1. Examine backend/pop3/pop3.go lines 107-112:
// POP3 messages are 1-indexed. We want newest first (highest ID first).
start := len(msgs) - int(offset)  // uint32 → int
if start <= 0 {
    return []backend.Email{}, nil
}

end := start - int(limit)  // uint32 → int
if end < 0 {
    end = 0
}
  1. On 32-bit arch, if offset = 0x80000000 (2147483648):
    • int(offset) = -2147483648 (negative!)
    • start = len(msgs) - (-2147483648) = overflow

Expected behavior Check for overflow before casting, or use int64 for intermediate calculations, or validate offset/limit are within reasonable bounds.

Additional context

  • Good first issue: straightforward bounds validation
  • Low severity: rare (most systems are 64-bit, POP3 unlikely to have 2B+ messages)
  • But still a correctness bug - could cause panic or wrong messages returned
  • Function signature: FetchEmails(_ context.Context, _ string, limit, offset uint32)

Suggested fix:

// Validate offset and limit are within reasonable bounds
if offset > uint32(len(msgs)) {
    return []backend.Email{}, nil
}
if limit > uint32(len(msgs)) {
    limit = uint32(len(msgs))
}

start := len(msgs) - int(offset)
if start <= 0 {
    return []backend.Email{}, nil
}

end := start - int(limit)
if end < 0 {
    end = 0
}

OS 32-bit systems (ARM, x86)

andrinoff avatar Apr 21 '26 09:04 andrinoff

This has had no activity for 30 days. It will be closed in 14 days unless updated. Comment or remove the stale label to keep it open.

floatpanebot avatar May 22 '26 06:05 floatpanebot

Closing due to inactivity. Reopen if still relevant.

floatpanebot avatar Jun 04 '26 19:06 floatpanebot