Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address CodeQL gripe about uncontrolled format string in handling of GEARMAND_PORT #411

Conversation

esabol
Copy link
Member

@esabol esabol commented Aug 6, 2024

This merge request addresses CodeQL's gripe about an "uncontrolled format string" alert around line 626 of libgearman-server/gearmand.cc. The alert suggests that, if a user sets the GEARMAND_PORT environment variable to a string with percent characters in it, for example, it could result in a buffer overflow and/or crash the program. (Refer to meta-issue #406.) This merge request addresses that by limiting the string to at most 5 characters (since 65535 is the maximum port number) and then truncating the string at the first non-digit character.

I believe this PR also addresses issue #320 with Kubernetes setting GEARMAND_PORT to unexpected values by default.

@esabol esabol mentioned this pull request Aug 6, 2024
@SpamapS
Copy link
Member

SpamapS commented Aug 25, 2024

Hi Ed. Thanks for taking this on. I don't love the approach of silently dropping non-numeric chars. The real problem is the way we're constructing the buffer for gearmand_gai_error. I'd much rather see that changed to be an argument with a %s as the format string.

@esabol
Copy link
Member Author

esabol commented Aug 25, 2024

@SpamapS wrote:

Hi Ed. Thanks for taking this on. I don't love the approach of silently dropping non-numeric chars. The real problem is the way we're constructing the buffer for gearmand_gai_error.

I suspect that getaddrinfo() is already silently dropping non-numeric characters in the port number, because that's the sort of thing that libc does all over the place, so this probably doesn't actually change much in that regard.

I'd much rather see that changed to be an argument with a %s as the format string.

It doesn't work that way. Or the gearmand_log_* functions are too labyrinthine for me to discern a way of doing that.

That said, I think changing the "%s:%s" on line 616 of libgearman-server/gearmand.cc to "%s:%d" might satisfy CodeQL's gripe. Would you be ok with that? That would only change the error message.

int length= snprintf(buffer, sizeof(buffer), "%s:%s", gearmand->host ? gearmand->host : "<any>", port->port);

Or we could get rid of lines 614 to 620 entirely and change line 626 from

return gearmand_gai_error(buffer, ret);

to simply gearmand_gai_error("getaddrinfo", ret);, which is exactly what the code does at

gearmand_gai_error("getaddrinfo", ret);

@SpamapS
Copy link
Member

SpamapS commented Aug 25, 2024

Actually no, I forgot, ports can be non-numeric! /etc/services defines names for some ports. So we can't just ditch the non-numerics.

Honestly, the way the code uses message/format interchangeably is a bit disturbing. format should always be protected and ideally is only programmer-input never user-input.

I don't know how far down the rabbit hole you're up for going, but if we could make a gearmand_gai_error alternative that takes both a format and a message, and use that instead of building the buffer, that would be the ideal IMO.

Otherwise, yeah, maybe just drop the detail in the error message.

@esabol
Copy link
Member Author

esabol commented Aug 25, 2024

It's not gearmand_gai_error() that's the problem. The problem is gearmand_log_perror(), I think, and I'm not willing to go down that rabbit hole. (It's usage is far too widespread.) But that gives me an idea. We could just change how gearmand_log_gai_error() calls gearmand_log_perror().... Revision forthcoming.

@esabol esabol force-pushed the address-CodeQL-gripe-about-uncontrolled-format-string branch from a5ed043 to 28b449a Compare August 25, 2024 18:03
@esabol esabol requested a review from SpamapS August 25, 2024 19:21
@esabol
Copy link
Member Author

esabol commented Aug 25, 2024

@SpamapS : Updated. Please review the latest revision.

libgearman-server/log.cc Outdated Show resolved Hide resolved
…f the GEARMAND_PORT environment variable by changing how gearmand_log_gai_error() calls gearmand_log_perror().
@esabol esabol force-pushed the address-CodeQL-gripe-about-uncontrolled-format-string branch from 28b449a to 3f9a48f Compare August 25, 2024 21:19
@esabol esabol requested a review from SpamapS August 25, 2024 21:33
@esabol esabol merged commit c24d899 into gearman:master Sep 24, 2024
17 checks passed
@esabol esabol deleted the address-CodeQL-gripe-about-uncontrolled-format-string branch September 24, 2024 04:04
@esabol
Copy link
Member Author

esabol commented Sep 24, 2024

ARGH! I hate GitHub Actions so much! All CI workflows are failing. It all worked a month ago!

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