Skip to content

Fix buffer overflow vulnerability in format()#26

Open
faratech wants to merge 1 commit intobo0ts:masterfrom
faratech:master
Open

Fix buffer overflow vulnerability in format()#26
faratech wants to merge 1 commit intobo0ts:masterfrom
faratech:master

Conversation

@faratech
Copy link

Summary

Fix a buffer overflow vulnerability in the format() function where user-controlled format strings (via +format argument) could overflow the fixed 391-byte output buffer.

Vulnerability Details

  • Type: Stack buffer overflow (CWE-120)
  • Attack Vector: Command line argument +format
  • Impact: Stack smashing / potential code execution

Proof of Concept

# Before fix - crashes with stack smashing
./ddate +"%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%.%."
*** stack smashing detected ***: terminated

Changes

  • Add bufsize parameter to format() function
  • Add bounds checking before all buffer writes
  • Replace strcpy with length-limited memcpy
  • Replace sprintf with snprintf
  • Check remaining buffer space in main loop condition

Output is now safely truncated if it would exceed buffer size.

The format() function used a fixed 391-byte buffer without bounds
checking. User-controlled format strings (via +format argument)
could overflow this buffer, causing stack smashing.

Changes:
- Add bufsize parameter to format()
- Add bounds checking before all buffer writes
- Replace strcpy with length-limited memcpy
- Replace sprintf with snprintf
- Check buffer space in main loop condition

The output is now safely truncated if it exceeds buffer size.
@danwdart
Copy link
Collaborator

That sounds reasonable enough to me. Anyone against?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants