Skip to content

Conversation

jnovy
Copy link
Contributor

@jnovy jnovy commented Jul 28, 2025

Replace unsafe strcpy() with proper length validation and strncpy() to prevent buffer overflow when copying socket paths to sockaddr_un.sun_path.

This prevents potential buffer overflow vulnerabilities when handling long socket paths.

Summary by Sourcery

Add bounds checking and safe copying for Unix domain socket paths in open_unix_domain_client_socket and open_unix_domain_socket to eliminate buffer overflow vulnerabilities.

Bug Fixes:

  • Prevent potential buffer overflows when copying Unix domain socket paths by validating path length before copying

Enhancements:

  • Replace unsafe strcpy() calls with bounded strncpy() and explicit null-termination
  • Return ENAMETOOLONG error if socket path exceeds sockaddr_un.sun_path size

Copy link

sourcery-ai bot commented Jul 28, 2025

Reviewer's Guide

Replaces unsafe strcpy calls when assigning to sockaddr_un.sun_path with explicit length validation, strncpy, and manual null-termination to prevent buffer overflows.

Class diagram for updated Unix domain socket path handling

classDiagram
    class sockaddr_un {
        char sun_path[108]
    }
    class libcrun_error_t
    class open_unix_domain_client_socket {
        +int open_unix_domain_client_socket(const char *path, int dgram, libcrun_error_t *err)
        +// Now checks path length before copying
    }
    class open_unix_domain_socket {
        +int open_unix_domain_socket(const char *path, int dgram, libcrun_error_t *err)
        +// Now checks path length before copying
    }
    sockaddr_un <.. open_unix_domain_client_socket : uses
    sockaddr_un <.. open_unix_domain_socket : uses
    libcrun_error_t <.. open_unix_domain_client_socket : error reporting
    libcrun_error_t <.. open_unix_domain_socket : error reporting
Loading

File-Level Changes

Change Details Files
Introduced bounds checking and safe copy for Unix domain socket paths
  • Validate path length against sun_path buffer size, returning ENAMETOOLONG error on overflow
  • Replace strcpy() with strncpy() limiting copy to sizeof(sun_path)-1
  • Add explicit null-termination of sun_path buffer
src/libcrun/utils.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jnovy - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

strcpy (addr.sun_path, path);
if (strlen (path) >= sizeof (addr.sun_path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you are already computing the strlen(). Could we just store it in a variable and then use memcpy instead? That would be faster (and we don't have to drop the check for strncpy in make syntax-check)

Replace unsafe strcpy() with proper length validation and memcpy()
to prevent buffer overflow when copying socket paths to sockaddr_un.sun_path.

This prevents potential buffer overflow vulnerabilities when handling
long socket paths.

Signed-off-by: Jindrich Novy <jnovy@redhat.com>
@jnovy
Copy link
Contributor Author

jnovy commented Jul 30, 2025

Replaced the strncpy() with memcpy() @giuseppe PTAL

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@giuseppe giuseppe merged commit 22643c3 into containers:main Jul 30, 2025
57 of 60 checks passed
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