You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by tr...@apache.org on 2010/04/29 17:26:08 UTC

svn commit: r939344 - in /httpd/mod_fcgid/trunk/modules/fcgid: fcgid_conf.c fcgid_global.h fcgid_pm.h fcgid_pm_main.c fcgid_pm_unix.c fcgid_pm_win.c fcgid_proc_unix.c fcgid_proc_win.c fcgid_proctbl.h mod_fcgid.c

Author: trawick
Date: Thu Apr 29 15:26:08 2010
New Revision: 939344

URL: http://svn.apache.org/viewvc?rev=939344&view=rev
Log:
clean up declarations of buffers for cmdline and executable paths
- now there are separate fcgid-specific constants for those limits
- cmdline limit is now larger than executable path limit

use sizeof(target buffer) as third param to apr_cpystrn instead of
expected size of the buffer, to help avoid future defects

add checks for truncated paths/cmdlines in order to generate a 
more helpful error message


Modified:
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_conf.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_global.h
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm.h
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_main.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_unix.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_win.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_unix.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_win.c
    httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proctbl.h
    httpd/mod_fcgid/trunk/modules/fcgid/mod_fcgid.c

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_conf.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_conf.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_conf.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_conf.c Thu Apr 29 15:26:08 2010
@@ -792,8 +792,16 @@ const char *set_wrapper_config(cmd_parms
 
     wrapper = apr_pcalloc(cmd->pool, sizeof(*wrapper));
 
+    if (strlen(path) >= FCGID_PATH_MAX) {
+        return "Executable path length exceeds compiled-in limit";
+    }
     wrapper->cgipath = apr_pstrdup(cmd->pool, path);
+
+    if (strlen(wrapper_cmdline) >= FCGID_CMDLINE_MAX) {
+        return "Command line length exceeds compiled-in limit";
+    }
     wrapper->cmdline = apr_pstrdup(cmd->pool, wrapper_cmdline);
+
     wrapper->inode = finfo.inode;
     wrapper->deviceid = finfo.device;
     wrapper->virtual = (virtual != NULL && !strcasecmp(virtual, WRAPPER_FLAG_VIRTUAL));

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_global.h
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_global.h?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_global.h (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_global.h Thu Apr 29 15:26:08 2010
@@ -26,8 +26,29 @@
 #include "fcgid_config.h"
 #endif
 
-#ifndef _POSIX_PATH_MAX
-#define _POSIX_PATH_MAX 255
+/* FCGID_PATH_MAX
+ * - includes terminating '\0'
+ * - based on minimum supported path length (on Unix at least)
+ * - should be used in declarations, but logic should use sizeof
+ *   wherever possible
+ */
+#ifndef FCGID_PATH_MAX
+#ifdef _POSIX_PATH_MAX
+#define FCGID_PATH_MAX _POSIX_PATH_MAX
+#else
+#define FCGID_PATH_MAX 256
+#endif
+#endif
+
+/* FCGID_CMDLINE_MAX
+ * - includes terminating '\0'
+ * - FCGID_PATH_MAX represents the executable, remainder represents
+ *   the args
+ * - should be used in declarations, but logic should use sizeof
+ *   wherever possible
+ */
+#ifndef FCGID_CMDLINE_MAX
+#define FCGID_CMDLINE_MAX (FCGID_PATH_MAX + 256)
 #endif
 
 #define fcgid_min(a,b)    (((a) < (b)) ? (a) : (b))

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm.h
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm.h?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm.h (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm.h Thu Apr 29 15:26:08 2010
@@ -21,8 +21,8 @@
 #include "fcgid_conf.h"
 
 typedef struct {
-    char cgipath[_POSIX_PATH_MAX];
-    char cmdline[_POSIX_PATH_MAX];
+    char cgipath[FCGID_PATH_MAX];
+    char cmdline[FCGID_CMDLINE_MAX];
     apr_ino_t inode;
     dev_t deviceid;
     const char *virtualhost;  /* Virtualhost granularity */

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_main.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_main.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_main.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_main.c Thu Apr 29 15:26:08 2010
@@ -501,7 +501,11 @@ fastcgi_spawn(fcgid_command * command, s
     /* Prepare to spawn */
     procnode->deviceid = command->deviceid;
     procnode->inode = command->inode;
-    apr_cpystrn(procnode->cmdline, command->cmdline, _POSIX_PATH_MAX);
+
+    /* no truncation should ever occur */
+    AP_DEBUG_ASSERT(sizeof procnode->cmdline > strlen(command->cmdline));
+    apr_cpystrn(procnode->cmdline, command->cmdline, sizeof procnode->cmdline);
+
     procnode->virtualhost = command->virtualhost;
     procnode->uid = command->uid;
     procnode->gid = command->gid;

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_unix.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_unix.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_unix.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_unix.c Thu Apr 29 15:26:08 2010
@@ -435,8 +435,12 @@ void procmgr_init_spawn_cmd(fcgid_comman
         command->userdir = 0;
     }
 
-    apr_cpystrn(command->cgipath, cmd_conf->cgipath, _POSIX_PATH_MAX);
-    apr_cpystrn(command->cmdline, cmd_conf->cmdline, _POSIX_PATH_MAX);
+    /* no truncation should ever occur */
+    AP_DEBUG_ASSERT(sizeof command->cgipath > strlen(cmd_conf->cgipath));
+    apr_cpystrn(command->cgipath, cmd_conf->cgipath, sizeof command->cgipath);
+    AP_DEBUG_ASSERT(sizeof command->cmdline > strlen(cmd_conf->cmdline));
+    apr_cpystrn(command->cmdline, cmd_conf->cmdline, sizeof command->cmdline);
+
     command->deviceid = cmd_conf->deviceid;
     command->inode = cmd_conf->inode;
     command->virtualhost = r->server->server_hostname;

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_win.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_win.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_win.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_pm_win.c Thu Apr 29 15:26:08 2010
@@ -132,8 +132,12 @@ void procmgr_init_spawn_cmd(fcgid_comman
 {
     memset(command, 0, sizeof(*command));
 
-    apr_cpystrn(command->cgipath, cmd_conf->cgipath, _POSIX_PATH_MAX);
-    apr_cpystrn(command->cmdline, cmd_conf->cmdline, _POSIX_PATH_MAX);
+    /* no truncation should ever occur */
+    AP_DEBUG_ASSERT(sizeof command->cgipath > strlen(cmd_conf->cgipath));
+    apr_cpystrn(command->cgipath, cmd_conf->cgipath, sizeof command->cgipath);
+    AP_DEBUG_ASSERT(sizeof command->cmdline > strlen(cmd_conf->cmdline));
+    apr_cpystrn(command->cmdline, cmd_conf->cmdline, sizeof command->cmdline);
+
     command->uid = (uid_t) - 1;
     command->gid = (gid_t) - 1;
     command->userdir = 0;

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_unix.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_unix.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_unix.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_unix.c Thu Apr 29 15:26:08 2010
@@ -200,7 +200,7 @@ apr_status_t proc_spawn_process(const ch
     char **proc_environ;
     struct sockaddr_un unix_addr;
     apr_procattr_t *procattr = NULL;
-    int argc;
+    int argc, len;
     const char *wargv[APACHE_ARG_MAX + 1];
     const char *word; /* For wrapper */
     const char *tmp;
@@ -223,14 +223,29 @@ apr_status_t proc_spawn_process(const ch
      */
 
     /* Generate a UNIX domain socket file path */
-    /* XXX It's nothing I can do if the socket dir is too long... */
     memset(&unix_addr, 0, sizeof(unix_addr));
     unix_addr.sun_family = AF_UNIX;
-    apr_snprintf(unix_addr.sun_path, sizeof(unix_addr.sun_path) - 1,
-                 "%s/%" APR_PID_T_FMT ".%d", sconf->sockname_prefix,
-                 getpid(), g_process_counter++);
+    len = apr_snprintf(unix_addr.sun_path, sizeof(unix_addr.sun_path),
+                       "%s/%" APR_PID_T_FMT ".%d", sconf->sockname_prefix,
+                       getpid(), g_process_counter++);
+
+    /* check for truncation of the socket path
+     *
+     * cheap but overly zealous check for sun_path overflow: if length of
+     * prepared string is at the limit, assume truncation
+     */
+    if (len + 1 == sizeof(unix_addr.sun_path)
+        || len >= sizeof procnode->socket_path) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, main_server,
+                     "mod_fcgid: socket path length exceeds compiled-in limits");
+        return APR_EGENERAL;
+    }
+
     apr_cpystrn(procnode->socket_path, unix_addr.sun_path,
                 sizeof(procnode->socket_path));
+
+    /* truncation already checked for in handler or FcgidWrapper parser */
+    AP_DEBUG_ASSERT(strlen(wargv[0]) < sizeof(procnode->executable_path));
     apr_cpystrn(procnode->executable_path, wargv[0],
                 sizeof(procnode->executable_path));
 
@@ -513,6 +528,9 @@ apr_status_t proc_connect_ipc(fcgid_proc
     /* Connect to fastcgi server */
     memset(&unix_addr, 0, sizeof(unix_addr));
     unix_addr.sun_family = AF_UNIX;
+
+    /* PM already made this check for truncation */
+    AP_DEBUG_ASSERT(sizeof unix_addr.sun_path > strlen(procnode->socket_path));
     apr_cpystrn(unix_addr.sun_path, procnode->socket_path,
                 sizeof(unix_addr.sun_path));
 

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_win.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_win.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_win.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proc_win.c Thu Apr 29 15:26:08 2010
@@ -68,7 +68,7 @@ apr_status_t proc_spawn_process(const ch
     apr_status_t rv;
     apr_file_t *file;
     char **proc_environ;
-    char sock_path[_POSIX_PATH_MAX];
+    char sock_path[FCGID_PATH_MAX];
     int argc;
     char *wargv[APACHE_ARG_MAX + 1], *word; /* For wrapper */
     const char *tmp;
@@ -110,8 +110,8 @@ apr_status_t proc_spawn_process(const ch
     apr_table_setn(procinfo->proc_environ, SHUTDOWN_EVENT_NAME,
                    apr_ltoa(procnode->proc_pool, (long) *finish_event));
 
-    /* Prepare the listen namedpipe file name */
-    apr_snprintf(sock_path, _POSIX_PATH_MAX - 1,
+    /* Prepare the listen namedpipe file name (no check for truncation) */
+    apr_snprintf(sock_path, sizeof sock_path,
                  "\\\\.\\pipe\\fcgidpipe-%u.%lu",
                  GetCurrentProcessId(), g_process_counter++);
 

Modified: httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proctbl.h
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proctbl.h?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proctbl.h (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/fcgid_proctbl.h Thu Apr 29 15:26:08 2010
@@ -42,11 +42,11 @@ typedef struct {
     int node_type;              /* the type of this node, used in fcgid_status_hook() only */
     apr_pool_t *proc_pool;      /* pool for process */
     apr_proc_t proc_id;         /* the process id */
-    char executable_path[_POSIX_PATH_MAX]; /* executable file path */
-    char socket_path[_POSIX_PATH_MAX];  /* cgi application socket path */
+    char executable_path[FCGID_PATH_MAX]; /* executable file path */
+    char socket_path[FCGID_PATH_MAX]; /* cgi application socket path */
     apr_ino_t inode;            /* cgi file inode */
     apr_dev_t deviceid;         /* cgi file device id */
-    char cmdline[_POSIX_PATH_MAX]; /* entire command line */
+    char cmdline[FCGID_CMDLINE_MAX]; /* entire command line */
     gid_t gid;                  /* for suEXEC */
     uid_t uid;                  /* for suEXEC */
     const char *virtualhost;      /* the virtualhost this process belongs to */

Modified: httpd/mod_fcgid/trunk/modules/fcgid/mod_fcgid.c
URL: http://svn.apache.org/viewvc/httpd/mod_fcgid/trunk/modules/fcgid/mod_fcgid.c?rev=939344&r1=939343&r2=939344&view=diff
==============================================================================
--- httpd/mod_fcgid/trunk/modules/fcgid/mod_fcgid.c (original)
+++ httpd/mod_fcgid/trunk/modules/fcgid/mod_fcgid.c Thu Apr 29 15:26:08 2010
@@ -244,8 +244,18 @@ static int fcgid_handler(request_rec * r
             }
         }
 
+        /* Dummy up a wrapper configuration, using the requested file as 
+         * both the executable path and command-line.
+         */
         wrapper_conf = apr_pcalloc(r->pool, sizeof(*wrapper_conf));
 
+        if (strlen(command) >= fcgid_min(FCGID_PATH_MAX, FCGID_CMDLINE_MAX)) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                          "mod_fcgid: Executable path length exceeds compiled-in limit: %s",
+                          command);
+            return HTTP_INTERNAL_SERVER_ERROR;
+        }
+
         wrapper_conf->cgipath = apr_pstrdup(r->pool, command);
         wrapper_conf->cmdline = wrapper_conf->cgipath;
         wrapper_conf->inode = r->finfo.inode;