-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
ext/pcntl: Refactor usage of strlcpy #17172
Conversation
ext/pcntl/pcntl.c
Outdated
strlcpy(*pair, ZSTR_VAL(key), ZSTR_LEN(key) + 1); | ||
strlcat(*pair, "=", pair_length); | ||
strlcat(*pair, Z_STRVAL_P(element), pair_length); | ||
const uint8_t equal_len = strlen("="); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure equal_len
existence is really vital but not against it either.
ext/pcntl/pcntl.c
Outdated
current_arg++; | ||
} ZEND_HASH_FOREACH_END(); | ||
*current_arg = NULL; | ||
} else { | ||
argv = emalloc(2 * sizeof(char *)); | ||
argv = safe_emalloc(2, sizeof(char *), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a necessary, this can't overflow anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never really know when one is meant to use the safe version compared to normal emalloc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If overflow by summation/multiplication is possible, use the safe version
ext/pcntl/pcntl.c
Outdated
zend_string_release(key); | ||
goto cleanup_env_vars; | ||
} | ||
// TODO Check key and element do not have nul bytes? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably yeah
ext/pcntl/pcntl.c
Outdated
strlcat(*pair, Z_STRVAL_P(element), pair_length); | ||
const uint8_t equal_len = strlen("="); | ||
size_t pair_length = ZSTR_LEN(element_str) + equal_len + ZSTR_LEN(key) + 1; | ||
ZEND_ASSERT(pair_length < SIZE_MAX); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion doesn't really make a lot of sense, pair_length is size_t after all...
Granted, the assertions that were here prior to this PR also didn't make a lot of sense.
ext/pcntl/pcntl.c
Outdated
strlcat(*pair, "=", pair_length); | ||
strlcat(*pair, Z_STRVAL_P(element), pair_length); | ||
const uint8_t equal_len = strlen("="); | ||
size_t pair_length = ZSTR_LEN(element_str) + equal_len + ZSTR_LEN(key) + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never open-code an addition of string lengths, unless you know they're bounded such that they can't overflow; or if you overflow-check them yourself.
In this case, you should keep using safe_emalloc and only perform this summation after that call.
If you don't, you risk integer overflow which can turn into heap buffer overflow.
We allocate the buffer, so we know that it will fit. Drive by refactorings and questions
c54e11c
to
c97e105
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one typo found, otherwise LGTM
Co-authored-by: Niels Dossche <[email protected]>
We allocate the buffer, so we know that it will fit.
Drive by refactorings and questions