source: branches/fc11-dev/server/common/patches/httpd-suexec-scripts.patch @ 1146

Last change on this file since 1146 was 1146, checked in by mitchb, 13 years ago
Version, context, etc. fixups to build Apache on Fedora 11
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] ) ] )
[1146]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-03 05:16:45.000000000 -0400
[842]49@@ -30,6 +30,9 @@
[298]50  *
51  */
52 
53+#define STATIC_CAT_PATH "/usr/local/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"
[1146]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>
[1146]67@@ -95,6 +99,7 @@
[1]68 {
69     /* variable name starts with */
70     "HTTP_",
71+    "HTTPS_",
72     "SSL_",
73 
74     /* variable name is */
[944]75@@ -245,9 +250,67 @@
[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",
110+    "JPG",
[315]111+    "xhtml",
[618]112+    "svg",
[944]113+    "xaml",
114+    "xap",
[298]115+    NULL
116+};
117+
118+static int is_static_extension(const char *file)
119+{
120+    const char *extension = strrchr(file, '.');
121+    const char **p;
122+    if (extension == NULL) return 0;
123+    for (p = static_extensions; *p; ++p) {
124+        if (strcmp(extension + 1, *p) == 0) return 1;
125+    }
126+    return 0;
127+}
128+
[842]129+static int is_php_extension(const char *file)
130+{
131+    const char *extension = strrchr(file, '.');
132+    if (extension == NULL) return 0;
133+    return strcmp(extension + 1, "php") == 0;
134+}
135+
[298]136 int main(int argc, char *argv[])
137 {
138     int userdir = 0;        /* ~userdir flag             */
[823]139+    int trusteddir = 0;     /* TRUSTED_DIRECTORY flag    */
140     uid_t uid;              /* user information          */
141     gid_t gid;              /* target group placeholder  */
142     char *target_uname;     /* target user name          */
[1146]143@@ -350,6 +413,20 @@
[823]144 #endif /*_OSD_POSIX*/
145 
146     /*
147+     * First check if this is an absolute path to the directory
148+     * of trusted executables. These are supposed to be security
149+     * audited to check parameters and validity on their own...
150+     */
151+    if (strstr(cmd, AP_TRUSTED_DIRECTORY) == cmd) {
152+        if (strstr(cmd, "/../") != NULL) {
153+            log_err("invalid command (%s)\n", cmd);
154+            exit(104);
155+        }
156+        trusteddir = 1;
157+        goto TRUSTED_DIRECTORY;
158+    }
159+
160+    /*
161      * Check for a leading '/' (absolute path) in the command to be executed,
162      * or attempts to back up out of the current directory,
163      * to protect against attacks.  If any are
[1146]164@@ -371,6 +448,7 @@
[823]165         userdir = 1;
166     }
167 
168+TRUSTED_DIRECTORY:
169     /*
170      * Error out if the target username is invalid.
171      */
[1146]172@@ -452,7 +530,7 @@
[103]173      * Error out if attempt is made to execute as root or as
174      * a UID less than AP_UID_MIN.  Tsk tsk.
175      */
176-    if ((uid == 0) || (uid < AP_UID_MIN)) {
177+    if ((uid == 0) || (uid < AP_UID_MIN && uid != 102)) {
178         log_err("cannot run as forbidden uid (%d/%s)\n", uid, cmd);
179         exit(107);
180     }
[1146]181@@ -484,6 +562,21 @@
[103]182         log_err("failed to setuid (%ld: %s)\n", uid, cmd);
183         exit(110);
184     }
[406]185+    if (is_selinux_enabled()) {
186+        if (uid == 102) {
187+            if (setexeccon("system_u:system_r:signup_t:s0") == -1) {
188+                log_err("failed to setexeccon (%ld: %s) to signup_t\n", uid, cmd);
189+                exit(201);
190+            }
191+        } else {
192+            if (setexeccon("user_u:user_r:user_t:s0") == -1) {
193+                log_err("failed to setexeccon (%ld: %s) to user_t\n", uid, cmd);
194+                exit(202);
195+            }
[405]196+        }
[103]197+    }
[908]198+
199+    setenv("HOME", target_homedir, 1);
[103]200 
201     /*
202      * Get the current working directory, as well as the proper
[1146]203@@ -506,6 +599,21 @@
[823]204             log_err("cannot get docroot information (%s)\n", target_homedir);
205             exit(112);
[1]206         }
[823]207+        size_t expected_len = strlen(target_homedir)+1+strlen(AP_USERDIR_SUFFIX)+1;
208+        char *expected = malloc(expected_len);
209+        snprintf(expected, expected_len, "%s/%s", target_homedir, AP_USERDIR_SUFFIX);
210+        if (strncmp(cwd, expected, expected_len-1) != 0) {
211+            log_err("error: file's directory not a subdirectory of user's home directory (%s, %s)\n", cwd, expected);
212+            exit(114);
213+        }
214+    }
215+    else if (trusteddir) {
216+        if (((chdir(AP_TRUSTED_DIRECTORY)) != 0) ||
217+            ((getcwd(dwd, AP_MAXPATH)) == NULL) |
218+            ((chdir(cwd)) != 0)) {
219+            log_err("cannot get docroot information (%s)\n", AP_TRUSTED_DIRECTORY);
220+            exit(112);
221+        }
[1]222     }
[823]223     else {
224         if (((chdir(AP_DOC_ROOT)) != 0) ||
[1146]225@@ -532,15 +640,17 @@
[1]226     /*
227      * Error out if cwd is writable by others.
228      */
229+#if 0
230     if ((dir_info.st_mode & S_IWOTH) || (dir_info.st_mode & S_IWGRP)) {
231         log_err("directory is writable by others: (%s)\n", cwd);
232         exit(116);
233     }
234+#endif
235 
236     /*
237      * Error out if we cannot stat the program.
238      */
239-    if (((lstat(cmd, &prg_info)) != 0) || (S_ISLNK(prg_info.st_mode))) {
240+    if (((lstat(cmd, &prg_info)) != 0) /*|| (S_ISLNK(prg_info.st_mode))*/) {
241         log_err("cannot stat program: (%s)\n", cmd);
242         exit(117);
243     }
[1146]244@@ -548,10 +658,12 @@
[1]245     /*
246      * Error out if the program is writable by others.
247      */
248+#if 0
249     if ((prg_info.st_mode & S_IWOTH) || (prg_info.st_mode & S_IWGRP)) {
250         log_err("file is writable by others: (%s/%s)\n", cwd, cmd);
251         exit(118);
252     }
253+#endif
254 
255     /*
256      * Error out if the file is setuid or setgid.
[1146]257@@ -565,6 +677,7 @@
[1]258      * Error out if the target name/group is different from
259      * the name/group of the cwd or the program.
260      */
261+#if 0
262     if ((uid != dir_info.st_uid) ||
263         (gid != dir_info.st_gid) ||
264         (uid != prg_info.st_uid) ||
[1146]265@@ -576,16 +689,33 @@
[1]266                 prg_info.st_uid, prg_info.st_gid);
267         exit(120);
268     }
269+#endif
270     /*
271      * Error out if the program is not executable for the user.
272      * Otherwise, she won't find any error in the logs except for
[842]273      * "[error] Premature end of script headers: ..."
274      */
275-    if (!(prg_info.st_mode & S_IXUSR)) {
276+    if (!is_static_extension(cmd) && !is_php_extension(cmd) &&
[873]277+        !(prg_info.st_mode & S_IXUSR)) {
[842]278         log_err("file has no execute permission: (%s/%s)\n", cwd, cmd);
[873]279         exit(121);
[842]280     }
[298]281 
282+    if (is_static_extension(cmd)) {
283+        argv[2] = STATIC_CAT_PATH;
284+        execv(STATIC_CAT_PATH, &argv[2]);
[1146]285+        log_err("(%d)%s: static_cat exec failed (%s)\n", errno, strerror(errno), argv[2]);
286+        exit(255);
[298]287+    }
[842]288+    if (is_php_extension(cmd)) {
289+        setenv("PHPRC", ".", 1);
290+        argv[1] = PHP_PATH;
291+        argv[2] = "-f";
292+        execv(PHP_PATH, &argv[1]);
[1146]293+        log_err("(%d)%s: php exec failed (%s)\n", errno, strerror(errno), argv[2]);
294+        exit(255);
[842]295+    }
[298]296+
[1146]297 #ifdef AP_SUEXEC_UMASK
[298]298     /*
[1146]299      * umask() uses inverse logic; bits are CLEAR for allowed access.
Note: See TracBrowser for help on using the repository browser.