GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fileutils likely overrunning write · Issue #134351 · python/cpython

fileutils likely overrunning write #134351

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
fileutils likely overrunning write#134351
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@odaysec
odaysec
opened on May 20, 2025
Issue body actions

Feature or enhancement

Proposal:

cpython/Python/fileutils.c

Line 2132 in f695eca

res = realpath(cpath, cresolved_path);

The program performs a buffer copy or write operation with no upper limit on the size of the copy. By analyzing the bounds of the expressions involved, it appears that certain inputs will cause a buffer overflow to occur in this case. In addition to causing program instability, techniques exist which may allow an attacker to use this vulnerability to execute arbitrary code.

Recommendation

Always control the length of buffer copy and buffer write operations. strncpy should be used over strcpy, snprintf over sprintf, and in other cases 'n-variant' functions should be preferred.

int sayHello(uint32_t userId)
{
	char buffer[17];

	if (userId > 9999) return USER_ID_OUT_OF_BOUNDS;

	// BAD: this message overflows the buffer if userId >= 1000,
	// as no space for the null terminator was accounted for
	sprintf(buffer, "Hello, user %d!", userId);

	MessageBox(hWnd, buffer, "New Message", MB_OK);
	
	return SUCCESS;
}

the call to sprintf writes a message of 14 characters (including the terminating null) plus the length of the string conversion of userId into a buffer with space for just 17 characters. While userId is checked to occupy no more than 4 characters when converted, there is no space in the buffer for the terminating null character if userId >= 1000. In this case, the null character overflows the buffer resulting in undefined behavior.

References

CERT C Coding Standard: STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator
CERT C++ Coding Standard: STR50-CPP. Guarantee that storage for strings has sufficient space for character data and the null terminator

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Activity

  1. odaysec
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 20, 2025
  2. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on May 20, 2025
  3. picnixz commented on May 20, 2025

    @picnixz
    picnixz
    on May 20, 2025
    Member
    More actions

    Actually, I think this can be considered a duplicate of #42400 (and according to Jelle's comment, it is, so I'm going to close this one as a duplicate).

  4. picnixz
    closed this as a duplicateof Avoid using unsafe realpath #42400 on May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.