Bug 44752 - Suexec does not correctly check that scripts are inside the docroot
Summary: Suexec does not correctly check that scripts are inside the docroot
Status: NEW
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: support (show other bugs)
Version: 2.5-HEAD
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2008-04-03 14:07 UTC by Stefan Fritsch
Modified: 2017-03-08 17:39 UTC (History)
2 users (show)



Attachments
patch for suexec (1.69 KB, patch)
2008-04-03 14:53 UTC, Stefan Fritsch
Details | Diff
more simple patch (363 bytes, patch)
2009-03-14 06:33 UTC, Yuya Tanaka
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Fritsch 2008-04-03 14:07:48 UTC
Suexec does not check correctly that the executed script is inside the 
docroot directory. It does 

        chdir(AP_DOC_ROOT)
        getcwd(dwd, AP_MAXPATH)

to get the docroot directory and then does a simple

        strncmp(cwd, dwd, strlen(dwd))

to compare it with the working directory. But getcwd returns the 
directory without a trailing slash (at least under linux). This means 
that, if AP_DOC_ROOT is set to e.g. /var/www, suexec will happily 
execute scripts under /var/www.bak . The same is true for userdir 
requests, i.e. /home/joe/public_html.bak will be accepted.


As Joe Orton pointed out, this is
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-1742

The advisory
http://labs.idefense.com/intelligence/vulnerabilities/display.php?id=511
also lists a race condition.
Comment 1 Stefan Fritsch 2008-04-03 14:53:39 UTC
Created attachment 21780 [details]
patch for suexec

This patch should fix both issues, but I am not sure if the fchdir part is portable.
Comment 2 Yuya Tanaka 2009-03-14 06:33:14 UTC
Created attachment 23383 [details]
more simple patch

This is simpler patch.
It only tests whether NULL or '/' is exist on the end of cwd string.
Comment 3 Stefan Fritsch 2009-03-15 01:20:58 UTC
(In reply to comment #2)
> Created an attachment (id=23383) [details]
> more simple patch
> 
> This is simpler patch.
> It only tests whether NULL or '/' is exist on the end of cwd string.

Have you tested this in the case that the script is located directly in the document root (and not in a subdir?)
Comment 4 Yuya Tanaka 2009-03-15 03:00:06 UTC
Oops.. I've forgotten to test that case...

I have just tested whether it execs /var/www/test_args.cgi with AP_DOC_ROOT="/var/www".
I also tested whether it doesn't exec /var/www-test/test_args.cgi.
And it works for me. :)