-
Notifications
You must be signed in to change notification settings - Fork 22
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
smbclient_state_free leads to segfault #5
Comments
Quick question: do you get the same error if you run PHP in cli mode (from the commandline?) Any errors in the log? I'll try to reproduce this shortly when I have a bit more time. |
Sorry, I should have mentioned that. No it doesn't happen in cli mode. Also, I've just found that if I run httpd manually rather than as a service then it also doesn't crash. So it seems to be linked to the init script (I'm using CentOS). I'll keep digging... |
Confirmed. When Apache is run (as root) from the command line with:
...all works perfectly. When run with:
...the segfaults happen. However, running:
...seems to be fine. |
Solved. It seems to require a HOME environment variable. Adding:
to /etc/init.d/httpd did it. I guess this is so that it can find smb.conf |
I'm fairly sure that the issue happens because the library is not properly threadsafe the PHP way. Specifically, the globals defined on line 63 and 64 should probably be guarded with PHP's ZTS macros to ensure thread safety. That whole area is something I don't know a lot about (it's all a black art as far as I can tell), but I'll try to read up on the dev docs and get a fix out. |
Re: fixed, if that's the cause, it would still be a very serious bug. You could check if that's really the cause by opening a shell, unsetting HOME, and running the script under the php cli. |
Yep. Unsetting HOME caused an instant segfault. |
When building with ZTS enabled, ensure that every thread gets its own private member of the PHP resource list pointers. Attempting to solve Github issue eduardok#5.
A missing I tried hard to reproduce the behavior you described, but failed. Now that you've isolated a test case with just php-cli, could you maybe run it under gdb or valgrind to get a backtrace of where exactly things go wrong? Tell me if you need pointers on how to do this. In the meantime, I followed my own hunch and committed some code on a test branch in my repo that isolates all the global variables (all two of them!) into per-thread containers. This means that each thread gets its own copy of the file and context resource lists, which might prevent race conditions or null pointer dereferencing (a likely cause of segfaults). Could you maybe compile that branch to see if it fixes the issue? |
Found some documentation that claims that resource list pointers don't need to be threadsafe. Makes sense. So my test branch fixes what isn't broken. |
Backtrace below...
|
Thanks. You may want to anonymize that. So it's a |
LOL, already done. I blame fatigue. |
So as I read the stack trace, we first call So I dove into the Samba source code. So from the looks of it, you found a Samba bug, for which the workaround is to make the config file easy to find by putting it in a default spot or by defining |
Btw, I was having a segfault in Apache with the libsmbclient 0.4 compiled on Mac OS X 10.9.4 using Homebrew PHP54 and Homebrew Samba3. Adding the HOME key to the Environment variables (per this thread) in EnvironmentVariables Now I get this with this code. |
The warning about the resource being a boolean occurs because |
Yea I gathered that. Nothing in the logs. |
Then we're in the twilight zone. As I see it, either Hmm. Could you please check with a |
Ok, will do some more investigation on it. Yes it is strange. I have the call in a try/catch also and it doesn't trigger the catch block. Will report back as soon as I have more info. |
Found this bug in Samba's Bugzilla about exactly this issue. |
I'd been working on context support but noticed that it's been added anyway which is great. However freeing the context seems to occasionally cause a segfault the next time that smbclient_opendir is called.
Example script:-
The first three or four requests are fine, after that it begins seemingly randomly segfaulting. Removing any mention of smbc_free_context from libsmbclient.c prevents the issue from happening but obviously isn't ideal because connections are then left open.
I'm testing this with PHP 5.5 on Apache Prefork.
Let me know if you need any more information.
The text was updated successfully, but these errors were encountered: