aMule Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

We're back! (IN POG FORM)

Author Topic: amuleweb 2.2.2: php include() crash  (Read 7057 times)

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
amuleweb 2.2.2: php include() crash
« on: August 10, 2008, 05:49:38 PM »

The php include() statement can't find the file to include, and crashes when invoked.

The calling code looks like this:
Code: [Select]
... HTML ...
<?php
include('utils.php');

... 
some inline php ...
?>

... HTML ...

Crash message and backtrace:
Code: [Select]
WARNING: include file [utils.php] can not be opened
ERROR in grammar syntax error after [)] near line 275

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb79e26c0 (LWP 3246)]
0x080ae844 in php_execute (node=0x1, result=0xbf9ec81c) at php_syntree.cpp:1790
1790 switch (node->type) {

(gdb) bt
#0  0x080ae844 in php_execute (node=0x1, result=0xbf9ec81c) at php_syntree.cpp:1790
#1  0x080c0b3a in CPhPLibContext::Execute (this=0x818e698, buf=0xbf9ec8b0) at php_core_lib.cpp:547
#2  0x080c0d82 in CPhpFilter (this=0xbf9ec8e3, server=0x8157d90, sess=0x816474c,
    file=0x818e558 "/home/boru/.aMule/webserver/litoral/amuleweb-main-shared.php", buff=0xbf9ec8b0)
    at php_core_lib.cpp:624
#3  0x08056c7f in CScriptWebServer::ProcessPhpRequest (this=0x8157d90,
    filename=0x818e558 "/home/boru/.aMule/webserver/litoral/amuleweb-main-shared.php", sess=0x816474c,
    size=@0xbf9eca18) at WebServer.cpp:1796
#4  0x0805d526 in CScriptWebServer::ProcessURL (this=0x8157d90, Data=@0xbf9ecad4) at WebServer.cpp:1917
#5  0x0808d394 in CWebSocket::OnRequestReceived (this=0x81936b0,
    pHeader=0x8188276 "HTTP/1.1\r\nHost: localhost:8081\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r"...,
    pData=0x0, dwDataLen=0) at WebSocket.cpp:224
#6  0x0808d97a in CWebSocket::OnInput (this=0x81936b0) at WebSocket.cpp:117
#7  0x0805f4fb in CWebServerBase::OnWebSocketEvent (this=0x8157d90, event=@0x8172d40) at WebServer.cpp:317
#8  0xb7da30e1 in wxAppConsole::HandleEvent () from /usr/lib/libwx_baseu-2.8.so.0
#9  0xb7e4ab88 in wxEvtHandler::ProcessEventIfMatches () from /usr/lib/libwx_baseu-2.8.so.0
#10 0xb7e4ace8 in wxEventHashTable::HandleEvent () from /usr/lib/libwx_baseu-2.8.so.0
#11 0xb7e4ae4f in wxEvtHandler::ProcessEvent () from /usr/lib/libwx_baseu-2.8.so.0
#12 0xb7e4b380 in wxEvtHandler::ProcessPendingEvents () from /usr/lib/libwx_baseu-2.8.so.0
#13 0xb7da3a7f in wxAppConsole::ProcessPendingEvents () from /usr/lib/libwx_baseu-2.8.so.0
#14 0x0808a891 in CamulewebApp::TextShell (this=0x8140078) at WebInterface.cpp:655
#15 0x08090e8d in CaMuleExternalConnector::ConnectAndRun (this=0x8140078, ProgName=@0xbf9ece20,
    ProgVersion=@0xbf9ece1c) at ../../../src/ExternalConnector.cpp:413
#16 0x08089bf0 in CamulewebApp::OnRun (this=0x8140078) at WebInterface.cpp:360
#17 0xb7ddf05a in wxEntry () from /usr/lib/libwx_baseu-2.8.so.0
#18 0xb7ddf107 in wxEntry () from /usr/lib/libwx_baseu-2.8.so.0
#19 0x08089cd6 in main (argc=Cannot access memory at address 0x0
) at WebInterface.cpp:333

(gdb) print node
$1 = (PHP_SYN_NODE *) 0x1
Logged

lfroen

  • Guest
Re: amuleweb 2.2.2: php include() crash
« Reply #1 on: August 11, 2008, 01:04:23 PM »

Quote
WARNING: include file [utils.php] can not be opened
ERROR in grammar syntax error after [)] near line 275

First of all - thanks for bug report, I will check the problem.

Now, before checking the code - amuleweb php engine doesn't handle well syntax errors. Most probably, this form of include() statement is not supported anyway - did you noticed "ERROR in grammar" message?
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #2 on: August 11, 2008, 03:13:37 PM »

From php_lexer.l:
Code: [Select]
"include" BEGIN(INCLUDE);
<INCLUDE>[ \t]*
<INCLUDE>\'[a-zA-Z_][a-zA-Z0-9_\.]*\' {
...
}
If I'm reading this correctly, there shouldn't be parantheses? For instance "include 'asdf.php';" instead of "include('asdf.php');"?

I'll try later today.
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #3 on: August 11, 2008, 05:07:02 PM »

Yes, no parantheses seem to be accepted. I get no crash nor syntax error with this:
Code: [Select]
include 'utils.php';
However, amuleweb can still not open the file.

Also, the regex defined in the lexer excludes filenames containing '-' and filenames with path (for instance 'scripts/utils.php'). Is this by design?
Logged

lfroen

  • Guest
Re: amuleweb 2.2.2: php include() crash
« Reply #4 on: August 11, 2008, 05:59:50 PM »

Two things to blame here:
1. Not very intelligent lexer
2. ";" in preprocessor directive. This confuses strlen, and php tries to open file named utils.php'. I think you can notice this in warning message.

Regarding filenames. Paths disabled on purpose - security reason. As for names: theoretically, every character except '/' is allowed in filename by most filesystems. Including non-english one. On practice I tried to implement most common cases. If this is too restricting and/or annoying - tell me what you need, I'll change regexp.
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #5 on: August 11, 2008, 11:02:53 PM »

Two things to blame here:
1. Not very intelligent lexer
2. ";" in preprocessor directive. This confuses strlen, and php tries to open file named utils.php'. I think you can notice this in warning message.

I can't see it in the warning. Using the syntax that works, I get this:
Code: [Select]
WARNING: include file [utils.php] can not be opened

Regarding filenames. Paths disabled on purpose - security reason. As for names: theoretically, every character except '/' is allowed in filename by most filesystems. Including non-english one. On practice I tried to implement most common cases. If this is too restricting and/or annoying - tell me what you need, I'll change regexp.

No biggie, it just would be nice to be able to group scripts in folders. Everything in one folder gets messy fast. :)
How about only allowing paths relative to the template root directory?

As for filenames I only miss the hyphen character, '-'. It's a common filename character and should be included.
Logged

lfroen

  • Guest
Re: amuleweb 2.2.2: php include() crash
« Reply #6 on: August 12, 2008, 06:43:29 AM »

Quote
No biggie, it just would be nice to be able to group scripts in folders. Everything in one folder gets messy fast.
How about only allowing paths relative to the template root directory?

As I say, the problem is security. You see, "." (dot) is naturally allowed, so if "/" is allowed too, this means, that foo/../../bar is valid filename (by matching regexp). There's no easy trick (I know about) to prevent this, but parse filenames.

Quote
I can't see it in the warning. Using the syntax that works, I get this:
This assume, of cause that "utils.php" actually exists and can be opened. I'll do some tests, maybe I'm missing something here.

Quote
As for filenames I only miss the hyphen character, '-'. It's a common filename character and should be included.
OK
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #7 on: August 12, 2008, 11:44:00 AM »

Quote
I can't see it in the warning. Using the syntax that works, I get this:
This assume, of cause that "utils.php" actually exists and can be opened. I'll do some tests, maybe I'm missing something here.

You assume correctly. :)
"utils.php" exists in the root of the template folder, and is readable by everyone.

According to the PHP manual the include statement parses in HTML mode, so included files must escape back to PHP mode:

Code: [Select]
<?php
... code ...
?>


Does amuleweb just include the contents directly so that the PHP tags are unnecessary?
Logged

lfroen

  • Guest
Re: amuleweb 2.2.2: php include() crash
« Reply #8 on: August 12, 2008, 04:06:49 PM »

Quote
According to the PHP manual the include statement parses in HTML mode, so included files must escape back to PHP mode:

You right, in real PHP. Sorry to disappoint you - amuleweb PHP can not parse HTML. In any form or taste or kind. Adding such ability is very complicated and doesn't serve any purpose in scope of this project.

"include" statement here is not event a "statement" in language terms - it's preprocessor directive. The difference is following:

include("foo"); - executed after parsing, in execution time (sorry for tautology). <<-- not supported
include "foo" - preprocessor directive, processed on lexer level, parser doesn't get to see it. <<-- OK, contents of foo considered directly included
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #9 on: August 12, 2008, 06:19:51 PM »

Ok, pure PHP in included files.

Thanks for taking the time to explain things. Let me know if you need any help with tracking down the "include" statement problem.
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #10 on: September 07, 2008, 02:58:35 PM »

Any progress on this?
Logged

lfroen

  • Guest
Re: amuleweb 2.2.2: php include() crash
« Reply #11 on: September 07, 2008, 09:22:26 PM »

Yes, I think I know that's a problem is, but I doubtful about solution. The problem is, that "include" looks for specified file in current directory. On the contrary, regular php files are located relatively to $server_root.
That's why it worked in php in standalone compile, but fails in amuleweb environment.
I will work on solution.
Logged

jared1999

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 19
Re: amuleweb 2.2.2: php include() crash
« Reply #12 on: September 08, 2008, 08:36:40 PM »

Excellent, that will enable much cleaner template code. Thank you! :D
Logged