httpd symlink problems

Denis Vlasenko vda.linux at googlemail.com
Sun Jun 3 21:26:39 UTC 2007


On Friday 01 June 2007 13:18, Alexander Kriegisch wrote:
> I use BB on a mipsel-based DSL/LAN/WLAN router (AVM Fritz!Box series).
> 
> There is an issue regarding httpd: When running with a home directory
> containing symlinked directories, function 'sendCgi' calls 'realpath' in
> order to convert a relative path name into an absolute one. So far, so
> good. What is bad about 'realpath' is the fact that it resolves symlinks
> instead of keeping them in the path. This alters the directory structure
> as it is seen by CGI scripts, for example. Thus files cannot be found.
> 
> I tested four other web servers, and they all refrain from resolving
> symlinks, thus work on AVM's directory structure. I think a user space
> program should not try and be smarter than the filesystem by eliminating
> symlinks. A behaviour similar to pwd.c (uses 'xgetcwd') would be
> desireable here.
> 
> I am attaching a patch somebody else created for BB 1.4.2 (but the
> behaviour is the same in 1.5.1), not to have it included in BB but to
> ask you if there is a more elegant way to solve this problem. If so,
> please fix it and publish a patch or at least notify me so I can get the
> patch from SVN.

I found the patch adding too much code.
Please try simpler one, which is attached.

Unfortunately I mixed it up with "globalization" patch and with a few
extra fixes.

Your problem is hopefully fixed specifically by these hunks:

-                /* SCRIPT_FILENAME required by PHP in CGI mode */
-               if (!realpath(purl + 1, realpath_buff))
-                       goto error_execing_cgi;
-               setenv1("SCRIPT_FILENAME", realpath_buff);
+
+               /* SCRIPT_FILENAME required by PHP in CGI mode */
+               fullpath = concat_path_file(home_httpd, purl);
+               setenv1("SCRIPT_FILENAME", fullpath);
...
+       home_httpd = xrealloc_getcwd_or_warn(NULL);
+       /* We do not "absolutize" path given by -h (home) opt.
+        * If user gives relative path in -h, $SCRIPT_FILENAME can end up
+        * relative too. */


If patch will not work in some unexpected way, this is where it could be broked
by this unrelated change (unlikely, but I think you need to know):

@@ -2022,12 +2050,12 @@ int httpd_main(int argc, char **argv)
 #if ENABLE_FEATURE_HTTPD_CGI
        {
                char *p = getenv("PATH");
-               p = xstrdup(p); /* if gets NULL, returns NULL */
+               /* env strings themself are not freed, no need to strdup(p): */
                clearenv();
                if (p)
-                       setenv1("PATH", p);
+                       putenv(p - 5);
                if (!(opt & OPT_INETD))
-                       setenv_long("SERVER_PORT", config->port);
+                       setenv_long("SERVER_PORT", tcp_port);
        }
 #endif


Other highlights:

+// FIXME: setenv leaks memory! (old values of env vars are leaked)
+// Thus we have a bug on NOMMU.
+// Need to use this instead:
+// [malloc +] putenv + (in child: exec) + (in parent: unsetenv [+ free])

Sad but true. Yet another problem on NOMMU, was there for all this time.

# make bloatcheck
function                                             old     new   delta
getLine                                               79      82      +3
home                                                   3       -      -3
config                                              1718    1714      -4
handle_sigalrm                                        32      26      -6
static.argp                                            8       -      -8
sendCgi                                             1929    1921      -8
checkPermIP                                           57      48      -9
sendHeaders                                          529     517     -12
checkPerm                                            380     363     -17
httpdVersion                                          30       -     -30
handleIncoming                                      2641    2577     -64
parse_conf                                          1444    1351     -93
httpd_main                                          1394    1226    -168
------------------------------------------------------------------------------
(add/remove: 0/3 grow/shrink: 1/9 up/down: 3/-422)           Total: -419 bytes

# size */*/httpd.o
   text    data     bss     dec     hex filename
  10648       0      12   10660    29a4 busybox.t3/networking/httpd.o
  10265       0       0   10265    2819 busybox.t4/networking/httpd.o

:)

Please test attached.
--
vda
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 4.patch
Type: text/x-diff
Size: 30989 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20070603/91c56ea8/attachment-0002.bin 


More information about the busybox mailing list