-
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
Fix #17776 LDAP_OPT_X_TLS_REQUIRE_CERT can't be overridden #17940
base: PHP-8.3
Are you sure you want to change the base?
Conversation
This is my personal favorite given the IMHO strange OpenLDAP API. |
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 is probably the better solution, at least better DX.
ext/ldap/ldap.c
Outdated
@@ -987,6 +987,17 @@ PHP_FUNCTION(ldap_connect) | |||
snprintf( url, urllen, "ldap://%s:" ZEND_LONG_FMT, host, port ); | |||
} | |||
|
|||
#ifdef LDAP_OPT_X_TLS_NEWCTX | |||
if (!memcmp(url, "ldaps:", 6)) { |
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.
Note that url
can be NULL, because url = host
and host
is nullable.
Also I prefer if you use a normal string comparison rather than memcmp
to avoid potential overreads. I don't know if that's possible (depends on the behaviour of ldap_is_ldap_url
) but better safe than sorry and it wouldn't be really that much of a performance difference 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.
done
I am sorry, but as mentioned in #17776 (comment) I meanwhile noticed that this issue affects LDAPS ( Most likely STARTTLS needs the following (untested!) adapted code block in #ifdef LDAP_OPT_X_TLS_NEWCTX
int val = 0;
/* ensure all pending TLS options are applied in a new context */
if (ldap_set_option(ld->link, LDAP_OPT_X_TLS_NEWCTX, &val) != LDAP_OPT_SUCCESS) {
php_error_docref(NULL, E_WARNING, "Could not create new security context");
}
#endif I also had a look to Postfix, there |
eb25825
to
9dde13a
Compare
@robert-scheck can you try this PR ? It seems OK to me
|
Sadly, don't understand why CI is failing on |
Yes, commit 9dde13a followed by commit 6466ed7 work for me here as well - thank you. For the record:
|
Hard to say…if the CA of the certificate used for STARTTLS isn't part of the operating system trust store, I would expect it to fail. Or does the CI export something like |
2226d2f
to
db4cd76
Compare
Trying without any change, only for CI |
55b6a46
to
2d79174
Compare
55ff888
to
881a9fa
Compare
more tests
4cb7cd4
to
2966b09
Compare
Summary: We now have 2 tests for ldaps and start_tls checking
Locally, both passes, default config was OK without this PR, running on a sane env/config Problems in CI
Other eyes welcome May be classified security issue? |
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.
While I'm not a qualified reviewer for the PHP project, this pull request addresses #17776 for me with the technically correct result on the PHP side: Refuses untrusted SSL certificate by default, overriding LDAP_OPT_X_TLS_REQUIRE_CERT
works for both, STARTTLS (ldap://
) and LDAPS (ldaps://
), no change/impact on plaintext LDAP connections.
For me, the previous behaviour (without this pull request) qualifies as a security flaw, because being not able to override LDAP_OPT_X_TLS_REQUIRE_CERT
using PHP and not receiving any error for the failed override (let alone not being able to set LDAP_OPT_X_TLS_NEWCTX
directly) may lead to unexpected MITM when working with (multiple) trusted and untrusted LDAPS connections. I admit that working with two or more LDAP connections in a PHP script and different values for LDAP_OPT_X_TLS_REQUIRE_CERT
might not be the typical everyday usage.
And I personally prefer this approach over LDAP_OPT_X_TLS_NEWCTX
in #17939, because the OpenLDAP API seems to be somewhat strange here, and this change just leads to the correct result for PHP developers, similar like for other bindings (I don't know if there is any other library binding requiring an equivalent to LDAP_OPT_X_TLS_NEWCTX
).
What I can not comment on is the PHP specific part.
Last but not least: Thank really you very much, @remicollet!
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.
Fix generally looks good to me , but no strong opinion on the target branch. A bit on the fence wrt the behaviour change. Not sure about security implications, I don't know enough about LDAP to judge that.
I will need to investigate it as it's not immediately clear to me from a very quick look. I will try to find some time for it later today or tomorrow. |
I have been looking into this and currently feel that it's more an API limitation and mainly a missing constant. I can get why this change is done and it might make sense but I'm a bit worried about the behavior change. I need to look more to the openldap source to see how this is handled in terms of resetting other options. I think we should be a bit careful here so I set some time for ldap to my weekly plan and will be looking into this more. I think I will have a bit better idea before the next release branching and will send some update here. |
Am I missing something? If you don't use I am very sorry if we're talking about two different things, but I really would like to understand what you are worried about exactly. |
It's reseting the ctx (freeing the old one and creating a new one) that gets recreated from the currently saved options (combined with global ones) so there is a chance that there are some edge cases that we might be missing. I want to just analyse the impact of that to make sure that we are not going to miss anything and accidentaly break someones code. |
There is no behavior change on a modern distro (perhaps thanks to openldap 2.6 or good config). The change was observed on CI (perhaps because of OpenLDAP 2.5 or poor config), where the only SSL test was failing, as cert check is now enabled by default, which makes sense. |
@remicollet So why do you even ask RM's if you are so confident that there is no behaviour change and no risk? :) |
If you feel that there is a security risk, you should open a new adivsory and not try to discuss it here - that should be always evaluated in private. |
It's also because security is not a question for RM but for the security team. |
In terms of older versions, I can see that 2.4 is still in Ubuntu 20.04 which is likely still used (even though support ends next month or so but think there is an extended one. I see also that 2.5 is in Ubuntu 22.04 which will be even more in use. So it sounds we need to make sure that it works there fine as well. |
I think I got it. I guess you were asking RM's because of the potential change on those versions. I will check the older versions in that case more. Thanks for the hint. |
As the security impact was mentioned here, I will then look into it as well and will open a new advisory if I feel, there is anything so it can be discussed further. But please try to not mention security related questions publicly in the future. As I said, if you have feeling that something might have security impact, just create a new advisory in the future. |
Previous tests run on Fedora with openldap 2.6.8 New set of tests on RHEL-8.10 with openldap 2.4.46 and Fedora with manually installed openldap 2.5.19 In all cases:
|
Alternative to #17939