Skip to content

ext/hash: Fix GH-16711: Segfault in mhash() #16713

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

Closed
wants to merge 2 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Nov 6, 2024

Rewrote part of how it works to handle all cases dealing with the mhash_to_hash struct.

@Girgias Girgias requested a review from nielsdos November 6, 2024 14:03
@Girgias Girgias linked an issue Nov 6, 2024 that may be closed by this pull request
@cmb69
Copy link
Member

cmb69 commented Nov 6, 2024

From a quick look, the only affected function is mhash(). Can't we just bail out if algo == NULL after

php-src/ext/hash/hash.c

Lines 1211 to 1217 in fde053b

/* need to convert the first parameter from int constant to string algorithm name */
if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
if (algorithm_lookup.hash_name) {
algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
}
}

@nielsdos
Copy link
Member

nielsdos commented Nov 6, 2024

I agree with @cmb69. This patch is too complex for a bugfix as it adds more sources of truth (we have to check the algorithm ID and name separately and assert they're consistent).

@cmb69
Copy link
Member

cmb69 commented Nov 6, 2024

IMO okay now. Could also raise a warning or throw an exception – I don't mind, since these functions are deprecated anyway.

@Girgias
Copy link
Member Author

Girgias commented Nov 6, 2024

IMO okay now. Could also raise a warning or throw an exception – I don't mind, since these functions are deprecated anyway.

Meh, they return false on error with no warnings, introducing one now seems kinda pointless. Also not sure how many people even use mhash.

@nielsdos
Copy link
Member

nielsdos commented Nov 6, 2024

Also not sure how many people even use mhash.

It's deprecated anyway, it doesn't make much sense to do more than the bare minimum keeping it working IMHO.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

@Girgias Girgias closed this in 1b379f5 Nov 10, 2024
@Girgias Girgias deleted the mhash-segfault branch November 10, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segmentation fault in mhash()
3 participants