source: trunk/server/common/patches/httpd-suexec-scripts.patch @ 1785

Last change on this file since 1785 was 1785, checked in by geofft, 13 years ago
suexec also needs to know about filetypes: add ttf and otf
File size: 9.0 KB
RevLine 
[1]1# scripts.mit.edu httpd suexec patch
[823]2# Copyright (C) 2006, 2007, 2008  Jeff Arnold <jbarnold@mit.edu>,
3#                                 Joe Presbrey <presbrey@mit.edu>,
4#                                 Anders Kaseorg <andersk@mit.edu>,
5#                                 Geoffrey Thomas <geofft@mit.edu>
[1]6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20#
21# See /COPYRIGHT in this repository for more information.
22#
[103]23--- httpd-2.2.2/support/Makefile.in.old 2005-07-06 19:15:34.000000000 -0400
24+++ httpd-2.2.2/support/Makefile.in     2007-01-20 17:12:51.000000000 -0500
25@@ -60,7 +60,7 @@
26
27 suexec_OBJECTS = suexec.lo
28 suexec: $(suexec_OBJECTS)
29-       $(LINK) $(suexec_OBJECTS)
30+       $(LINK) -lselinux $(suexec_OBJECTS)
31
32 htcacheclean_OBJECTS = htcacheclean.lo
33 htcacheclean: $(htcacheclean_OBJECTS)
[823]34--- httpd-2.2.2/configure.in.old        2007-07-17 10:48:25.000000000 -0400
35+++ httpd-2.2.2/configure.in    2008-08-29 08:15:41.000000000 -0400
36@@ -559,6 +559,10 @@
37 APACHE_HELP_STRING(--with-suexec-userdir,User subdirectory),[
38   AC_DEFINE_UNQUOTED(AP_USERDIR_SUFFIX, "$withval", [User subdirectory] ) ] )
39 
40+AC_ARG_WITH(suexec-trusteddir,
41+APACHE_HELP_STRING(--with-suexec-trusteddir,Trusted SuExec directory),[
42+  AC_DEFINE_UNQUOTED(AP_TRUSTED_DIRECTORY, "$withval", [Trusted SuExec directory] ) ] )
43+
44 AC_ARG_WITH(suexec-docroot,
45 APACHE_HELP_STRING(--with-suexec-docroot,SuExec root directory),[
46   AC_DEFINE_UNQUOTED(AP_DOC_ROOT, "$withval", [SuExec root directory] ) ] )
[1259]47--- httpd-2.2.11/support/suexec.c.old   2008-11-30 10:47:31.000000000 -0500
48+++ httpd-2.2.11/support/suexec.c       2009-06-08 09:02:17.000000000 -0400
[842]49@@ -30,6 +30,9 @@
[298]50  *
51  */
52 
[1590]53+#define STATIC_CAT_PATH "/usr/bin/static-cat"
[842]54+#define PHP_PATH "/usr/bin/php-cgi"
[298]55+
56 #include "apr.h"
57 #include "ap_config.h"
58 #include "suexec.h"
[1259]59@@ -46,6 +49,7 @@
[103]60 #include <stdio.h>
61 #include <stdarg.h>
62 #include <stdlib.h>
63+#include <selinux/selinux.h>
64 
65 #ifdef HAVE_PWD_H
66 #include <pwd.h>
[1259]67@@ -95,6 +99,7 @@
[1]68 {
69     /* variable name starts with */
70     "HTTP_",
71+    "HTTPS_",
72     "SSL_",
73 
74     /* variable name is */
[1785]75@@ -245,9 +250,71 @@
[298]76     environ = cleanenv;
77 }
78 
79+static const char *static_extensions[] = {
80+    "html",
81+    "css",
82+    "gif",
83+    "jpg",
84+    "png",
85+    "htm",
86+    "jpeg",
87+    "js",
88+    "ico",
89+    "xml",
90+    "xsl",
91+    "tiff",
92+    "tif",
93+    "tgz",
94+    "tar",
95+    "jar",
96+    "zip",
97+    "pdf",
98+    "ps",
99+    "doc",
100+    "xls",
101+    "ppt",
102+    "swf",
103+    "mp3",
104+    "mov",
105+    "wmv",
106+    "mpg",
107+    "mpeg",
108+    "avi",
109+    "il",
[315]110+    "xhtml",
[618]111+    "svg",
[944]112+    "xaml",
113+    "xap",
[1464]114+    "wav",
115+    "mid",
116+    "midi",
[1785]117+    "ttf",
118+    "otf",
[298]119+    NULL
120+};
121+
122+static int is_static_extension(const char *file)
123+{
124+    const char *extension = strrchr(file, '.');
125+    const char **p;
126+    if (extension == NULL) return 0;
127+    for (p = static_extensions; *p; ++p) {
[1464]128+        if (strcasecmp(extension + 1, *p) == 0) return 1;
[298]129+    }
130+    return 0;
131+}
132+
[842]133+static int is_php_extension(const char *file)
134+{
135+    const char *extension = strrchr(file, '.');
136+    if (extension == NULL) return 0;
137+    return strcmp(extension + 1, "php") == 0;
138+}
139+
[298]140 int main(int argc, char *argv[])
141 {
142     int userdir = 0;        /* ~userdir flag             */
[823]143+    int trusteddir = 0;     /* TRUSTED_DIRECTORY flag    */
144     uid_t uid;              /* user information          */
145     gid_t gid;              /* target group placeholder  */
146     char *target_uname;     /* target user name          */
[1169]147@@ -268,6 +331,7 @@
148      * Start with a "clean" environment
149      */
150     clean_env();
151+    setenv("JAVA_TOOL_OPTIONS", "-Xmx128M", 1); /* scripts.mit.edu local hack */
[1259]152 
[1169]153     prog = argv[0];
154     /*
[1259]155@@ -350,6 +414,20 @@
[823]156 #endif /*_OSD_POSIX*/
157 
158     /*
159+     * First check if this is an absolute path to the directory
160+     * of trusted executables. These are supposed to be security
161+     * audited to check parameters and validity on their own...
162+     */
163+    if (strstr(cmd, AP_TRUSTED_DIRECTORY) == cmd) {
164+        if (strstr(cmd, "/../") != NULL) {
165+            log_err("invalid command (%s)\n", cmd);
166+            exit(104);
167+        }
168+        trusteddir = 1;
169+        goto TRUSTED_DIRECTORY;
170+    }
171+
172+    /*
173      * Check for a leading '/' (absolute path) in the command to be executed,
174      * or attempts to back up out of the current directory,
175      * to protect against attacks.  If any are
[1259]176@@ -371,6 +449,7 @@
[823]177         userdir = 1;
178     }
179 
180+TRUSTED_DIRECTORY:
181     /*
182      * Error out if the target username is invalid.
183      */
[1259]184@@ -452,7 +531,7 @@
[103]185      * Error out if attempt is made to execute as root or as
186      * a UID less than AP_UID_MIN.  Tsk tsk.
187      */
188-    if ((uid == 0) || (uid < AP_UID_MIN)) {
[1474]189+    if ((uid == 0) || (uid < AP_UID_MIN && uid != 102)) { /* uid 102 = signup  */
[103]190         log_err("cannot run as forbidden uid (%d/%s)\n", uid, cmd);
191         exit(107);
192     }
[1474]193@@ -484,6 +563,7 @@
[103]194         log_err("failed to setuid (%ld: %s)\n", uid, cmd);
195         exit(110);
196     }
[908]197+    setenv("HOME", target_homedir, 1);
[103]198 
199     /*
200      * Get the current working directory, as well as the proper
[1259]201@@ -506,6 +600,21 @@
[823]202             log_err("cannot get docroot information (%s)\n", target_homedir);
203             exit(112);
[1]204         }
[823]205+        size_t expected_len = strlen(target_homedir)+1+strlen(AP_USERDIR_SUFFIX)+1;
206+        char *expected = malloc(expected_len);
207+        snprintf(expected, expected_len, "%s/%s", target_homedir, AP_USERDIR_SUFFIX);
208+        if (strncmp(cwd, expected, expected_len-1) != 0) {
209+            log_err("error: file's directory not a subdirectory of user's home directory (%s, %s)\n", cwd, expected);
210+            exit(114);
211+        }
212+    }
213+    else if (trusteddir) {
214+        if (((chdir(AP_TRUSTED_DIRECTORY)) != 0) ||
215+            ((getcwd(dwd, AP_MAXPATH)) == NULL) |
216+            ((chdir(cwd)) != 0)) {
217+            log_err("cannot get docroot information (%s)\n", AP_TRUSTED_DIRECTORY);
218+            exit(112);
219+        }
[1]220     }
[823]221     else {
222         if (((chdir(AP_DOC_ROOT)) != 0) ||
[1259]223@@ -532,15 +641,17 @@
[1]224     /*
225      * Error out if cwd is writable by others.
226      */
227+#if 0
228     if ((dir_info.st_mode & S_IWOTH) || (dir_info.st_mode & S_IWGRP)) {
229         log_err("directory is writable by others: (%s)\n", cwd);
230         exit(116);
231     }
232+#endif
233 
234     /*
235      * Error out if we cannot stat the program.
236      */
237-    if (((lstat(cmd, &prg_info)) != 0) || (S_ISLNK(prg_info.st_mode))) {
238+    if (((lstat(cmd, &prg_info)) != 0) /*|| (S_ISLNK(prg_info.st_mode))*/) {
239         log_err("cannot stat program: (%s)\n", cmd);
240         exit(117);
241     }
[1259]242@@ -548,10 +659,12 @@
[1]243     /*
244      * Error out if the program is writable by others.
245      */
246+#if 0
247     if ((prg_info.st_mode & S_IWOTH) || (prg_info.st_mode & S_IWGRP)) {
248         log_err("file is writable by others: (%s/%s)\n", cwd, cmd);
249         exit(118);
250     }
251+#endif
252 
253     /*
254      * Error out if the file is setuid or setgid.
[1259]255@@ -565,6 +678,7 @@
[1]256      * Error out if the target name/group is different from
257      * the name/group of the cwd or the program.
258      */
259+#if 0
260     if ((uid != dir_info.st_uid) ||
261         (gid != dir_info.st_gid) ||
262         (uid != prg_info.st_uid) ||
[1355]263@@ -576,12 +690,14 @@
[1]264                 prg_info.st_uid, prg_info.st_gid);
265         exit(120);
266     }
267+#endif
268     /*
269      * Error out if the program is not executable for the user.
270      * Otherwise, she won't find any error in the logs except for
[842]271      * "[error] Premature end of script headers: ..."
272      */
273-    if (!(prg_info.st_mode & S_IXUSR)) {
274+    if (!is_static_extension(cmd) && !is_php_extension(cmd) &&
[873]275+        !(prg_info.st_mode & S_IXUSR)) {
[842]276         log_err("file has no execute permission: (%s/%s)\n", cwd, cmd);
[873]277         exit(121);
[842]278     }
[1590]279@@ -614,6 +730,23 @@
[1355]280     /*
281      * Execute the command, replacing our image with its own.
282      */
[298]283+    if (is_static_extension(cmd)) {
[1590]284+        if (setenv("PATH_TRANSLATED", cmd, 1) != 0) {
285+            log_err("setenv failed\n");
286+            exit(255);
287+        }
288+        execl(STATIC_CAT_PATH, STATIC_CAT_PATH, (const char *)NULL);
289+        log_err("(%d)%s: static-cat exec failed (%s)\n", errno, strerror(errno), STATIC_CAT_PATH);
[1259]290+        exit(255);
[298]291+    }
[842]292+    if (is_php_extension(cmd)) {
293+        setenv("PHPRC", ".", 1);
294+        argv[1] = PHP_PATH;
295+        argv[2] = "-f";
296+        execv(PHP_PATH, &argv[1]);
[1354]297+        log_err("(%d)%s: php exec failed (%s)\n", errno, strerror(errno), argv[1]);
[1259]298+        exit(255);
[842]299+    }
[1355]300 #ifdef NEED_HASHBANG_EMUL
301     /* We need the #! emulation when we want to execute scripts */
302     {
Note: See TracBrowser for help on using the repository browser.