X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=support%2Fsuexec.c;h=d25ca8430ecbb9bfebff0433f9406a98873db17c;hb=ba6999ccefdd5fb979ed2da6bcee2c1591ec49e1;hp=cb048992825fe47a2f9f1a066ceabed1d31472eb;hpb=cf9772a8fcd3e1e734f582db8ccee41d95635fbc;p=apache
diff --git a/support/suexec.c b/support/suexec.c
index cb04899282..d25ca8430e 100644
--- a/support/suexec.c
+++ b/support/suexec.c
@@ -1,55 +1,17 @@
-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
*
- * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
- * reserved.
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
- * "This product includes software developed by the
- * Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself,
- * if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- * nor may "Apache" appear in their name, without prior written
- * permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * .
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
/*
@@ -58,8 +20,8 @@
***********************************************************************
*
* NOTE! : DO NOT edit this code!!! Unless you know what you are doing,
- * editing this code might open up your system in unexpected
- * ways to would-be crackers. Every precaution has been taken
+ * editing this code might open up your system in unexpected
+ * ways to would-be crackers. Every precaution has been taken
* to make this code as safe as possible; alter it at your own
* risk.
*
@@ -84,6 +46,9 @@
#include
#include
#include
+#if APR_HAVE_FCNTL_H
+#include
+#endif
#ifdef HAVE_PWD_H
#include
@@ -93,29 +58,6 @@
#include
#endif
-/*
- ***********************************************************************
- * There is no initgroups() in QNX, so I believe this is safe :-)
- * Use cc -osuexec -3 -O -mf -DQNX suexec.c to compile.
- *
- * May 17, 1997.
- * Igor N. Kovalenko -- infoh@mail.wplus.net
- ***********************************************************************
- */
-
-#if defined(NEED_INITGROUPS)
-int initgroups(const char *name, gid_t basegid)
-{
- /* QNX and MPE do not appear to support supplementary groups. */
- return 0;
-}
-#endif
-
-#if defined(SUNOS4)
-extern char *sys_errlist[];
-#define strerror(x) sys_errlist[(x)]
-#endif
-
#if defined(PATH_MAX)
#define AP_MAXPATH PATH_MAX
#elif defined(MAXPATHLEN)
@@ -129,7 +71,7 @@ extern char *sys_errlist[];
extern char **environ;
static FILE *log = NULL;
-char *safe_env_lst[] =
+static const char *const safe_env_lst[] =
{
/* variable name starts with */
"HTTP_",
@@ -145,7 +87,6 @@ char *safe_env_lst[] =
"DOCUMENT_PATH_INFO=",
"DOCUMENT_ROOT=",
"DOCUMENT_URI=",
- "FILEPATH_INFO=",
"GATEWAY_INTERFACE=",
"HTTPS=",
"LAST_MODIFIED=",
@@ -174,6 +115,7 @@ char *safe_env_lst[] =
"SERVER_ADDR=",
"SERVER_PORT=",
"SERVER_PROTOCOL=",
+ "SERVER_SIGNATURE=",
"SERVER_SOFTWARE=",
"UNIQUE_ID=",
"USER_NAME=",
@@ -189,7 +131,11 @@ static void err_output(int is_error, const char *fmt, va_list ap)
struct tm *lt;
if (!log) {
+#if defined(_LARGEFILE64_SOURCE) && HAVE_FOPEN64
+ if ((log = fopen64(AP_LOG_EXEC, "a")) == NULL) {
+#else
if ((log = fopen(AP_LOG_EXEC, "a")) == NULL) {
+#endif
fprintf(stderr, "suexec failure: could not open log file\n");
perror("fopen");
exit(1);
@@ -255,9 +201,9 @@ static void clean_env(void)
*/
char **envp = environ;
char *empty_ptr = NULL;
-
+
environ = &empty_ptr; /* VERY safe environment */
-
+
if ((cleanenv = (char **) calloc(AP_ENVBUF, sizeof(char *))) == NULL) {
log_err("failed to malloc memory for environment\n");
exit(120);
@@ -433,13 +379,15 @@ int main(int argc, char *argv[])
log_err("invalid target group name: (%s)\n", target_gname);
exit(106);
}
- gid = gr->gr_gid;
- actual_gname = strdup(gr->gr_name);
}
else {
- gid = atoi(target_gname);
- actual_gname = strdup(target_gname);
+ if ((gr = getgrgid(atoi(target_gname))) == NULL) {
+ log_err("invalid target group id: (%s)\n", target_gname);
+ exit(106);
+ }
}
+ gid = gr->gr_gid;
+ actual_gname = strdup(gr->gr_name);
#ifdef _OSD_POSIX
/*
@@ -467,7 +415,7 @@ int main(int argc, char *argv[])
}
}
#endif /*_OSD_POSIX*/
-
+
/*
* Save these for later since initgroups will hose the struct
*/
@@ -476,7 +424,7 @@ int main(int argc, char *argv[])
target_homedir = strdup(pw->pw_dir);
/*
- * Log the transaction here to be sure we have an open log
+ * Log the transaction here to be sure we have an open log
* before we setuid().
*/
log_no_err("uid: (%s/%s) gid: (%s/%s) cmd: %s\n",
@@ -633,18 +581,27 @@ int main(int argc, char *argv[])
umask(AP_SUEXEC_UMASK);
#endif /* AP_SUEXEC_UMASK */
- /*
- * Be sure to close the log file so the CGI can't
- * mess with it. If the exec fails, it will be reopened
- * automatically when log_err is called. Note that the log
- * might not actually be open if AP_LOG_EXEC isn't defined.
- * However, the "log" cell isn't ifdef'd so let's be defensive
- * and assume someone might have done something with it
- * outside an ifdef'd AP_LOG_EXEC block.
- */
+ /* Be sure to close the log file so the CGI can't mess with it. */
if (log != NULL) {
+#if APR_HAVE_FCNTL_H
+ /*
+ * ask fcntl(2) to set the FD_CLOEXEC flag on the log file,
+ * so it'll be automagically closed if the exec() call succeeds.
+ */
+ fflush(log);
+ setbuf(log, NULL);
+ if ((fcntl(fileno(log), F_SETFD, FD_CLOEXEC) == -1)) {
+ log_err("error: can't set close-on-exec flag");
+ exit(122);
+ }
+#else
+ /*
+ * In this case, exec() errors won't be logged because we have already
+ * dropped privileges and won't be able to reopen the log file.
+ */
fclose(log);
log = NULL;
+#endif
}
/*