[php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6

From: Date: Sun, 01 Nov 2026 08:27:08 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143115@lists.php.net to get a copy of this message
Author: Jakub Zelenka (bukka) Date: 2026-11-01T09:26:55+01:00 Commit: https://github.com/php/php-src/commit/d5b3917cec432847e13fe663a80a2bc404d7d586 Raw diff: https://github.com/php/php-src/commit/d5b3917cec432847e13fe663a80a2bc404d7d586.diff Merge branch 'PHP-8.5' into PHP-8.6 * PHP-8.5: Revert "Update LiteSpeed API to v8.4" Changed paths: M sapi/litespeed/lsapi_main.c M sapi/litespeed/lsapilib.c M sapi/litespeed/lsapilib.h Diff: diff --git a/sapi/litespeed/lsapi_main.c b/sapi/litespeed/lsapi_main.c index 1056f9c34c96..60d1ca358b55 100644 --- a/sapi/litespeed/lsapi_main.c +++ b/sapi/litespeed/lsapi_main.c @@ -26,8 +26,6 @@ #include "lsapilib.h" #include "lsapi_main_arginfo.h" -#include <errno.h> -#include <limits.h> #include <stdio.h> #include <stdlib.h> @@ -75,8 +73,6 @@ static int ignore_php_ini = 0; static char * argv0 = NULL; static int engine = 1; static int parse_user_ini = 0; -static size_t argv0_suffix_capacity = 0; -static int max_cmd_script_path_len = 45; #ifdef ZTS zend_compiler_globals *compiler_globals; @@ -135,43 +131,27 @@ static void sapi_lsapi_ini_defaults(HashTable *configuration_hash) /* {{{ sapi_lsapi_ub_write */ static size_t sapi_lsapi_ub_write(const char *str, size_t str_length) { - ssize_t ret; - size_t written = 0; + int ret; + int remain; if ( lsapi_mode ) { - while (written < str_length) { - size_t chunk = str_length - written; - if (chunk > (size_t)INT_MAX) { - chunk = (size_t)INT_MAX; - } - ret = LSAPI_Write(str + written, (ssize_t)chunk); - if (ret <= 0) { - php_handle_aborted_connection(); - break; - } - written += (size_t)ret; - if ((size_t)ret != chunk) { - php_handle_aborted_connection(); - break; - } + ret = LSAPI_Write( str, str_length ); + if ( ret < str_length ) { + php_handle_aborted_connection(); + return str_length - ret; } } else { - while (written < str_length) { - size_t chunk = str_length - written; - if (chunk > (size_t)INT_MAX) { - chunk = (size_t)INT_MAX; - } - ret = write(1, str + written, chunk); - if ( ret > 0 ) { - written += ret; - } else if ( ret == -1 && errno == EINTR ) { - continue; - } else { + remain = str_length; + while( remain > 0 ) { + ret = write( 1, str, remain ); + if ( ret <= 0 ) { php_handle_aborted_connection(); - break; + return str_length - remain; } + str += ret; + remain -= ret; } } - return written; + return str_length; } /* }}} */ @@ -379,28 +359,6 @@ static const http_error http_error_codes[] = { }; -static void lsapi_append_response_header(const char *header, size_t len) -{ - const char *colon; - size_t name_len; - char msg[160]; - - if (!header) - return; - if (len <= INT_MAX && LSAPI_AppendRespHeader((char *)header, (int)len) == 0) - return; - - colon = memchr(header, ':', len); - name_len = colon ? (size_t)(colon - header) : len; - if (name_len > 64) - name_len = 64; - snprintf(msg, sizeof(msg), - "LiteSpeed SAPI: dropped response header '%.*s' (%zu bytes)\n", - (int)name_len, header, len); - LSAPI_Write_Stderr(msg, strlen(msg)); -} - - static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers) { char buf[SAPI_LSAPI_MAX_HEADER_LENGTH]; @@ -410,8 +368,8 @@ static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers) int response_status = SG(sapi_headers).http_response_code; if (SG(request_info).no_headers == 1) { - return LSAPI_FinalizeRespHeaders() == -1 - ? SAPI_HEADER_SEND_FAILED : SAPI_HEADER_SENT_SUCCESSFULLY; + LSAPI_FinalizeRespHeaders(); + return SAPI_HEADER_SENT_SUCCESSFULLY; } if (SG(sapi_headers).http_response_code != 200) @@ -457,8 +415,7 @@ static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers) } if (!has_status) { - if (len > 0) - lsapi_append_response_header(buf, (size_t)len); + LSAPI_AppendRespHeader( buf, len ); ignore_status = 1; } } @@ -472,7 +429,7 @@ static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers) ) { if (!ignore_status) { ignore_status = 1; - lsapi_append_response_header(h->header, h->header_len); + LSAPI_AppendRespHeader(h->header, h->header_len); } } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 && strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0 @@ -480,14 +437,14 @@ static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers) h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos); continue; } else { - lsapi_append_response_header(h->header, h->header_len); + LSAPI_AppendRespHeader(h->header, h->header_len); } } h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos); } - return LSAPI_FinalizeRespHeaders() == -1 - ? SAPI_HEADER_SEND_FAILED : SAPI_HEADER_SENT_SUCCESSFULLY; + LSAPI_FinalizeRespHeaders(); + return SAPI_HEADER_SENT_SUCCESSFULLY; } /* @@ -502,9 +459,6 @@ static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers) sapi_header_struct *h; zend_llist_position pos; - if (!lsapi_mode) - return SAPI_HEADER_SENT_SUCCESSFULLY; - if ( mod_lsapi_mode ) { /* mod_lsapi mode */ return sapi_lsapi_send_headers_like_cgi(sapi_headers); @@ -512,13 +466,12 @@ static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers) /* Legacy mode */ if ( lsapi_mode ) { - if (LSAPI_SetRespStatus(SG(sapi_headers).http_response_code) == -1) - return SAPI_HEADER_SEND_FAILED; + LSAPI_SetRespStatus( SG(sapi_headers).http_response_code ); h = zend_llist_get_first_ex(&sapi_headers->headers, &pos); while (h) { if ( h->header_len > 0 ) { - lsapi_append_response_header(h->header, h->header_len); + LSAPI_AppendRespHeader(h->header, h->header_len); } h = zend_llist_get_next_ex(&sapi_headers->headers, &pos); } @@ -528,16 +481,15 @@ static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers) char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH]; hd = sapi_get_default_content_type(); - len = snprintf( headerBuf, sizeof(headerBuf), + len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1, "Content-type: %s", hd ); efree(hd); - if ( len > 0 && (size_t)len < sizeof(headerBuf) ) - lsapi_append_response_header( headerBuf, len ); + LSAPI_AppendRespHeader( headerBuf, len ); } } - return LSAPI_FinalizeRespHeaders() == -1 - ? SAPI_HEADER_SEND_FAILED : SAPI_HEADER_SENT_SUCCESSFULLY; + LSAPI_FinalizeRespHeaders(); + return SAPI_HEADER_SENT_SUCCESSFULLY; } @@ -548,34 +500,16 @@ static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers) static void sapi_lsapi_log_message(const char *message, int syslog_type_int) { char buf[8192]; - size_t len; - - if (!message) - return; - len = strlen(message); - if ( len == 0 || message[len - 1] != '\n' ) + int len = strlen( message ); + if ( *(message + len - 1 ) != '\n' ) { - if (len > sizeof(buf) - 2) - len = sizeof(buf) - 2; - memcpy( buf, message, len ); - buf[len++] = '\n'; + snprintf( buf, 8191, "%s\n", message ); message = buf; + if (len > 8191) + len = 8191; + ++len; } - if (lsapi_mode) { - (void)LSAPI_Write_Stderr(message, (ssize_t)len); - } else { - size_t written = 0; - while (written < len) { - ssize_t ret = write(STDERR_FILENO, message + written, - len - written); - if (ret > 0) - written += (size_t)ret; - else if (ret == -1 && errno == EINTR) - continue; - else - break; - } - } + LSAPI_Write_Stderr( message, len ); } /* }}} */ @@ -655,7 +589,7 @@ static int sapi_lsapi_activate(void) static sapi_module_struct lsapi_sapi_module = { "litespeed", - "LiteSpeed V8.4", + "LiteSpeed V8.3", php_lsapi_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ @@ -714,28 +648,19 @@ static void init_request_info( void ) static int lsapi_execute_script(void) { zend_file_handle file_handle; - const char *script_path; - size_t copy_len; - size_t title_len; + char *p; + int len; zend_stream_init_filename(&file_handle, SG(request_info).path_translated); file_handle.primary_script = true; - script_path = SG(request_info).path_translated; - if (argv0 && argv0_suffix_capacity > 1) { - title_len = (size_t)max_cmd_script_path_len; - if (title_len >= argv0_suffix_capacity) - title_len = argv0_suffix_capacity - 1; - - copy_len = strlen(script_path); - if (copy_len >= title_len) - script_path += copy_len - (title_len - 1); - else - title_len = copy_len + 1; - - argv0[0] = ':'; - memcpy(argv0 + 1, script_path, title_len - 1); - argv0[title_len] = '\0'; - } + p = argv0; + *p++ = ':'; + len = strlen( SG(request_info).path_translated ); + if ( len > 45 ) + len = len - 45; + else + len = 0; + memccpy( p, SG(request_info).path_translated + len, 0, 46 ); php_execute_script(&file_handle); zend_destroy_file_handle(&file_handle); @@ -848,8 +773,7 @@ static int lsapi_module_main(int show_source) clean_onexit = 1; - if (argv0) - memset( argv0, 0, argv0_suffix_capacity ); + memset( argv0, 0, 46 ); } zend_end_try(); return 0; } @@ -867,7 +791,7 @@ static int alter_ini( const char * pKey, int keyLen, const char * pValue, int va int type = ZEND_INI_PERDIR; int stage = PHP_INI_STAGE_RUNTIME; - if (pKey && pValue && keyLen >= 3 && valLen >= 0 && '\001' == *pKey) { + if ( '\001' == *pKey ) { ++pKey; if ( *pKey == 4 ) { type = ZEND_INI_SYSTEM; @@ -890,7 +814,7 @@ static int alter_ini( const char * pKey, int keyLen, const char * pValue, int va --keyLen; if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 )) { - if ( valLen > 0 && *pValue == '0' ) + if ( *pValue == '0' ) engine = 0; } else @@ -910,7 +834,7 @@ static void user_config_cache_entry_dtor(zval *el) { user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el); zend_hash_destroy(&entry->user_config); - pefree(entry, 1); + free(entry); } static void user_config_cache_init(void) @@ -1118,10 +1042,7 @@ static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_c int is_outside_of_docroot = !docroot_len || ctx->path_len < docroot_len || - strncmp(ctx->path, ctx->doc_root, docroot_len) != 0 || - (ctx->path_len > docroot_len && - !IS_SLASH(ctx->doc_root[docroot_len - 1]) && - !IS_SLASH(ctx->path[docroot_len])); + strncmp(ctx->path, ctx->doc_root, docroot_len) != 0; if (is_outside_of_docroot) { php_parse_user_ini_file(ctx->path, PG(user_ini_filename), @@ -1258,35 +1179,21 @@ static int parse_opt( int argc, char * argv[], int *climode, ++p; switch( c ) { case 'b': - { - char *bind; if ( p >= argend ) { fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n"); return -1; } - bind = strdup(*p++); - if (!bind) - return -1; - free(*php_bind); - *php_bind = bind; + *php_bind = strdup(*p++); break; - } case 'c': - { - char *ini_path; if ( p >= argend ) { fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n"); return -1; } - ini_path = strdup(*p++); - if (!ini_path) - return -1; - free(*php_ini_path); - *php_ini_path = ini_path; + *php_ini_path = strdup( *p++ ); break; - } case 's': source_highlight = 1; break; @@ -1516,17 +1423,8 @@ void start_children( int children ) if ( s_stop ) { break; } - pid = wait(&status); - if (pid > 0) - --running; - else if (pid == -1 && errno == EINTR) - continue; - else if (pid == -1 && errno == ECHILD) - running = 0; - else { - perror("php (waiting for child)"); - break; - } + pid = wait( &status ); + running--; } kill( -getpgrp(), SIGUSR1 ); exit( 0 ); @@ -1535,10 +1433,8 @@ void start_children( int children ) void setArgv0( int argc, char * argv[] ) { char * p; - char * area_end; int i; - area_end = argv[0] + strlen( argv[0] ); - argv0 = area_end; + argv0 = argv[0] + strlen( argv[0] ); p = argv0; while(( p > argv[0] )&&( p[-1] != '/')) --p; @@ -1548,11 +1444,6 @@ void setArgv0( int argc, char * argv[] ) memset( argv[0] + ( argv0 - p ), 0, p - argv[0] ); argv0 = argv[0] + (argv0 - p); } - /* The title may also use argv[1..] that the kernel laid out right after - * argv[0]; they are cleared below. */ - for( i = 1; i < argc && argv[i] == area_end + 1; ++i ) - area_end = argv[i] + strlen( argv[i] ); - argv0_suffix_capacity = (size_t)(area_end - argv0) + 1; for( i = 1; i < argc; ++i ) { memset( argv[i], 0, strlen( argv[i] ) ); @@ -1592,8 +1483,6 @@ int main( int argc, char * argv[] ) if (argc > 1 ) { if ( parse_opt( argc, argv, &climode, &php_ini_path, &php_bind ) == -1 ) { - free(php_ini_path); - free(php_bind); return 1; } } @@ -1635,16 +1524,11 @@ int main( int argc, char * argv[] ) #ifdef ZTS tsrm_shutdown(); #endif - free(php_ini_path); - free(php_bind); return FAILURE; } if ( climode ) { - ret = cli_main(argc, argv); - free(php_ini_path); - free(php_bind); - return ret; + return cli_main(argc, argv); } if ( php_bind ) { @@ -1660,31 +1544,13 @@ int main( int argc, char * argv[] ) } } - if ( LSAPI_Init() == -1 ) { - fprintf( stderr, "Failed to initialize LSAPI\n" ); - php_module_shutdown(); -#ifdef ZTS - tsrm_shutdown(); -#endif - free(php_ini_path); - free(php_bind); - return FAILURE; - } + LSAPI_Init(); #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__) int is_criu = LSCRIU_Init(); // Must be called before the regular init as it unsets the parameters. #endif - if ( LSAPI_Init_Env_Parameters( NULL ) == -1 ) { - fprintf( stderr, "Failed to initialize LSAPI environment\n" ); - php_module_shutdown(); -#ifdef ZTS - tsrm_shutdown(); -#endif - free(php_ini_path); - free(php_bind); - return FAILURE; - } + LSAPI_Init_Env_Parameters( NULL ); lsapi_mode = 1; slow_script_msec = LSAPI_Get_Slow_Req_Msecs(); @@ -1735,8 +1601,6 @@ int main( int argc, char * argv[] ) #ifdef ZTS tsrm_shutdown(); #endif - free(php_ini_path); - free(php_bind); return ret; } @@ -1759,12 +1623,6 @@ static PHP_MINIT_FUNCTION(litespeed) else if (*p == '0' || 0 == strcasecmp(p, "off")) do_clean_shutdown = 0; } - p = getenv("LSAPI_MAX_CMD_SCRIPT_PATH_LEN"); - if (p) { - int len = atoi(p); - if (len > 0 && len < 256) - max_cmd_script_path_len = len; - } /* * mod_lsapi always sets this env var, * so we can detect mod_lsapi mode with its presence. @@ -1833,26 +1691,25 @@ PHP_FUNCTION(litespeed_response_headers) RETURN_FALSE; } + if (!&SG(sapi_headers).headers) { + RETURN_FALSE; + } array_init(return_value); h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos); while (h) { if ( h->header_len > 0 ) { - p = memchr( h->header, ':', h->header_len ); - len = p ? p - h->header : 0; - if (len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) { + p = strchr( h->header, ':' ); + len = p - h->header; + if (p && len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) { memmove( headerBuf, h->header, len ); while( len > 0 && (isspace((unsigned char)headerBuf[len - 1])) ) { --len; } headerBuf[len] = 0; if ( len ) { - do { - ++p; - } while ((size_t)(p - h->header) < h->header_len && - isspace((unsigned char)*p)); - add_assoc_stringl_ex(return_value, headerBuf, len, p, - h->header_len - (p - h->header)); + while(isspace((unsigned char)*++p)); + add_assoc_string_ex(return_value, headerBuf, len, p); } } } diff --git a/sapi/litespeed/lsapilib.c b/sapi/litespeed/lsapilib.c index bd83a1487f6d..ff70a1896930 100644 --- a/sapi/litespeed/lsapilib.c +++ b/sapi/litespeed/lsapilib.c @@ -174,9 +174,6 @@ LSAPI_Request g_req = static char s_secret[24]; static LSAPI_On_Timer_pf s_proc_group_timer_cb = NULL; -static LSAPI_Fork_Hook_pf s_fork_before = NULL; -static LSAPI_Fork_Hook_pf s_fork_after_parent = NULL; -static LSAPI_Fork_Hook_pf s_fork_after_child = NULL; void Flush_RespBuf_r( LSAPI_Request * pReq ); static int lsapi_reopen_stderr(const char *p); @@ -376,7 +373,7 @@ static void lsapi_enable_core_dump(void) mib[0] = CTL_KERN; mib[1] = KERN_NOSUIDCOREDUMP; if (sysctl(mib, 2, NULL, 0, &set, len) == 0) { - s_enable_core_dump = 1; + s_enable_core_dump = 1; } #endif @@ -566,21 +563,12 @@ static inline int allocateBuf( LSAPI_Request * pReq, int size ) static int allocateIovec( LSAPI_Request * pReq, int n ) { - ptrdiff_t toWriteOff = 0; - ptrdiff_t curOff = 0; - if (n <= 0) - return -1; - if (pReq->m_pIovec) - { - toWriteOff = pReq->m_pIovecToWrite - pReq->m_pIovec; - curOff = pReq->m_pIovecCur - pReq->m_pIovec; - } struct iovec * p = (struct iovec *)realloc( pReq->m_pIovec, sizeof(struct iovec) * n ); if ( !p ) return -1; - pReq->m_pIovecToWrite = p + toWriteOff; - pReq->m_pIovecCur = p + curOff; + pReq->m_pIovecToWrite = p + ( pReq->m_pIovecToWrite - pReq->m_pIovec ); + pReq->m_pIovecCur = p + ( pReq->m_pIovecCur - pReq->m_pIovec ); pReq->m_pIovec = p; pReq->m_pIovecEnd = p + n; return 0; @@ -589,15 +577,10 @@ static int allocateIovec( LSAPI_Request * pReq, int n ) static int allocateRespHeaderBuf( LSAPI_Request * pReq, int size ) { - ptrdiff_t posOff = 0; - if (size <= 0) - return -1; - if (pReq->m_pRespHeaderBuf) - posOff = pReq->m_pRespHeaderBufPos - pReq->m_pRespHeaderBuf; char * p = (char *)realloc( pReq->m_pRespHeaderBuf, size ); if ( !p ) return -1; - pReq->m_pRespHeaderBufPos = p + posOff; + pReq->m_pRespHeaderBufPos = p + ( pReq->m_pRespHeaderBufPos - pReq->m_pRespHeaderBuf ); pReq->m_pRespHeaderBuf = p; pReq->m_pRespHeaderBufEnd = p + size; return 0; @@ -650,8 +633,8 @@ static inline int isPipe( int fd ) { char achPeer[128]; socklen_t len = 128; - if ((getpeername(fd, (struct sockaddr *)achPeer, &len) != 0 ) - && (errno == ENOTCONN || errno == ENOTSOCK)) + if (( getpeername( fd, (struct sockaddr *)achPeer, &len ) != 0 )&& + ( errno == ENOTCONN )) return 0; else return 1; @@ -661,30 +644,22 @@ static inline int isPipe( int fd ) static int parseEnv( struct LSAPI_key_value_pair * pEnvList, int count, char **pBegin, char * pEnd ) { - int i; - int keyLen = 0, valLen = 0; - size_t remaining; - - if (count < 0 || count > 8192 || !pBegin || !*pBegin - || !pEnd || *pBegin > pEnd || (count && !pEnvList)) + struct LSAPI_key_value_pair * pEnvEnd; + int keyLen = 0, valLen = 0; + if ( count > 8192 ) return -1; - for (i = 0; i < count; ++i, ++pEnvList) + pEnvEnd = pEnvList + count; + while( pEnvList != pEnvEnd ) { - remaining = (size_t)(pEnd - *pBegin); - if (remaining < 4) + if ( pEnd - *pBegin < 4 ) return -1; keyLen = *((unsigned char *)((*pBegin)++)); keyLen = (keyLen << 8) + *((unsigned char *)((*pBegin)++)); valLen = *((unsigned char *)((*pBegin)++)); valLen = (valLen << 8) + *((unsigned char *)((*pBegin)++)); - if (( !keyLen )||( !valLen )) - return -1; - remaining = (size_t)(pEnd - *pBegin); - if ((size_t)keyLen > remaining - || (size_t)valLen > remaining - (size_t)keyLen) + if ( *pBegin + keyLen + valLen > pEnd ) return -1; - if ((*pBegin)[keyLen - 1] != '\0' - || (*pBegin)[keyLen + valLen - 1] != '\0') + if (( !keyLen )||( !valLen )) return -1; pEnvList->pKey = *pBegin; @@ -694,9 +669,9 @@ static int parseEnv( struct LSAPI_key_value_pair * pEnvList, int count, pEnvList->keyLen = keyLen - 1; pEnvList->valLen = valLen - 1; + ++pEnvList; } - if ((size_t)(pEnd - *pBegin) < 4 - || memcmp( *pBegin, "\0\0\0\0", 4 ) != 0 ) + if ( memcmp( *pBegin, "\0\0\0\0", 4 ) != 0 ) return -1; *pBegin += 4; return 0; @@ -735,7 +710,7 @@ static inline void fixEndian( LSAPI_Request * pReq ) static void fixHeaderIndexEndian( LSAPI_Request * pReq ) { int i; - for( i = 0; i <= H_TRANSFER_ENCODING; ++i ) + for( i = 0; i < H_TRANSFER_ENCODING; ++i ) { if ( pReq->m_pHeaderIndex->m_headerOff[i] ) { @@ -768,16 +743,13 @@ static int validateHeaders( LSAPI_Request * pReq ) { int totalLen = pReq->m_pHeader->m_httpHeaderLen; int i; - if (totalLen < 0) - return -1; - for(i = 0; i <= H_TRANSFER_ENCODING; ++i) + for(i = 0; i < H_TRANSFER_ENCODING; ++i) { if ( pReq->m_pHeaderIndex->m_headerOff[i] ) { - int off = pReq->m_pHeaderIndex->m_headerOff[i]; - int len = pReq->m_pHeaderIndex->m_headerLen[i]; - if (off < 0 || off >= totalLen || len < 0 - || len >= totalLen - off) + if (pReq->m_pHeaderIndex->m_headerOff[i] > totalLen + || pReq->m_pHeaderIndex->m_headerLen[i] + + pReq->m_pHeaderIndex->m_headerOff[i] > totalLen) return -1; } } @@ -788,12 +760,10 @@ static int validateHeaders( LSAPI_Request * pReq ) pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders; while( pCur < pEnd ) { - if (pCur->nameOff < 0 || pCur->nameLen <= 0 - || pCur->nameOff >= totalLen - || pCur->nameLen >= totalLen - pCur->nameOff - || pCur->valueOff < 0 || pCur->valueLen < 0 - || pCur->valueOff >= totalLen - || pCur->valueLen >= totalLen - pCur->valueOff) + if (pCur->nameOff > totalLen + || pCur->nameOff + pCur->nameLen > totalLen + || pCur->valueOff > totalLen + || pCur->valueOff + pCur->valueLen > totalLen) return -1; ++pCur; } @@ -825,33 +795,30 @@ static int (*fp_lve_leave)(struct liblve *, uint32_t *) = NULL; static int (*fp_lve_jail)( struct passwd *, char *) = NULL; static int lsapi_load_lve_lib(void) { - s_liblve = dlopen("liblve.so.0", RTLD_NOW | RTLD_GLOBAL); - if (!s_liblve) + s_liblve = DL_LOAD("liblve.so.0"); + if (s_liblve) { - lsapi_log("LSAPI: configured LVE library could not be loaded: %s\n", - dlerror()); - return -1; - } - - fp_lve_is_available = dlsym(s_liblve, "lve_is_available"); - if (fp_lve_is_available && !(*fp_lve_is_available)()) - { - uid_t uid = getuid(); - if (uid) + fp_lve_is_available = dlsym(s_liblve, "lve_is_available"); + if (dlerror() == NULL) { - if (setreuid(s_uid, uid) == 0) + if ( !(*fp_lve_is_available)() ) { - int available = (*fp_lve_is_available)(); - if (setreuid(uid, s_uid) == -1) - return -1; - if (available) - return 0; + int uid = getuid(); + if ( uid ) + { + if (setreuid( s_uid, uid )) {}; + if ( !(*fp_lve_is_available)() ) + s_enable_lve = 0; + if (setreuid( uid, s_uid )) {}; + } } } - lsapi_log("LSAPI: configured LVE support is unavailable\n"); - return -1; } - return 0; + else + { + s_enable_lve = LSAPI_LVE_DISABLED; + } + return (s_liblve)? 0 : -1; } @@ -866,23 +833,11 @@ static int init_lve_ex(void) fp_lve_leave = dlsym(s_liblve, "lve_leave"); if ( s_enable_lve >= LSAPI_CAGEFS_ENABLED ) fp_lve_jail = dlsym(s_liblve, "jail" ); - if (!fp_lve_instance_init || !fp_lve_enter) - { - lsapi_log("LSAPI: required LVE symbols are unavailable\n"); - return -1; - } - if (s_enable_lve >= LSAPI_CAGEFS_ENABLED && !fp_lve_jail) - lsapi_log("LSAPI: CageFS jail symbol is unavailable; " - "continuing with LVE limits only\n"); if ( s_lve == NULL ) { rc = (*fp_lve_instance_init)(NULL); - if (rc <= 0 || rc > 1024 * 1024) - return -1; s_lve = malloc(rc); - if (!s_lve) - return -1; } rc = (*fp_lve_instance_init)(s_lve); if (rc != 0) @@ -903,16 +858,7 @@ static int init_lve_ex(void) static int readSecret( const char * pSecretFile ) { struct stat st; - size_t readLen = 0; - ssize_t len; - int flags = O_RDONLY; -#ifdef O_CLOEXEC - flags |= O_CLOEXEC; -#endif -#ifdef O_NOFOLLOW - flags |= O_NOFOLLOW; -#endif - int fd = open(pSecretFile, flags); + int fd = open( pSecretFile, O_RDONLY , 0600 ); if ( fd == -1 ) { lsapi_log("LSAPI: failed to open secret file: %s!\n", pSecretFile ); @@ -924,34 +870,25 @@ static int readSecret( const char * pSecretFile ) close( fd ); return -1; } - if (!S_ISREG(st.st_mode) || st.st_uid != s_uid) +/* + if ( st.st_uid != s_uid ) { - lsapi_log("LSAPI: secret file type or owner check failure: %s!\n", - pSecretFile); + lsapi_log("LSAPI: file owner check failure: %s!\n", pSecretFile ); close( fd ); return -1; } +*/ if ( st.st_mode & 0077 ) { lsapi_log("LSAPI: file permission check failure: %s\n", pSecretFile ); close( fd ); return -1; } - while (readLen < 16) + if ( read( fd, s_secret, 16 ) < 16 ) { - len = read(fd, s_secret + readLen, 16 - readLen); - if (len > 0) - readLen += len; - else if (len == -1 && errno == EINTR) - continue; - else - { - lsapi_log("LSAPI: failed to read secret from secret file: %s\n", - pSecretFile); - memset(s_secret, 0, sizeof(s_secret)); - close(fd); - return -1; - } + lsapi_log("LSAPI: failed to read secret from secret file: %s\n", pSecretFile ); + close( fd ); + return -1; } close( fd ); return 0; @@ -1057,9 +994,12 @@ static int lsapi_initLVE(void) } if ( s_enable_lve && !s_uid ) { - if (lsapi_load_lve_lib() == -1) - return -1; - return init_lve_ex(); + lsapi_load_lve_lib(); + if ( s_enable_lve ) + { + return init_lve_ex(); + } + } return 0; } @@ -1090,12 +1030,10 @@ static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * p } } #endif - if (!uid || !gid) - { - errno = EPERM; - LSAPI_perror_r(pReq, "LSAPI: refusing root UID or GID", NULL); - return -1; - } + //if ( !uid || !gid ) //do not allow root + //{ + // return -1; + //} #if defined(__FreeBSD__ ) || defined(__NetBSD__) || defined(__OpenBSD__) \ || defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) @@ -1103,6 +1041,12 @@ static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * p lsapi_enable_core_dump(); #endif + rv = setgid(gid); + if (rv == -1) + { + LSAPI_perror_r(pReq, "LSAPI: setgid()", NULL); + return -1; + } if ( pw && (pw->pw_gid == gid )) { rv = initgroups( pw->pw_name, gid ); @@ -1118,15 +1062,8 @@ static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * p if (rv == -1) { LSAPI_perror_r(pReq, "LSAPI: setgroups()", NULL); - return -1; } } - rv = setgid(gid); - if (rv == -1) - { - LSAPI_perror_r(pReq, "LSAPI: setgid()", NULL); - return -1; - } if ( pChroot ) { rv = chroot( pChroot ); @@ -1135,11 +1072,6 @@ static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * p LSAPI_perror_r(pReq, "LSAPI: chroot()", NULL); return -1; } - if (chdir("/") == -1) - { - LSAPI_perror_r(pReq, "LSAPI: chdir() after chroot", NULL); - return -1; - } } rv = setuid(uid); if (rv == -1) @@ -1154,13 +1086,12 @@ static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * p return 0; } -static int lsapi_suexec_auth(char *pAuth, int len, char *pUgid, int ugidLen) +static int lsapi_suexec_auth( LSAPI_Request *pReq, + char * pAuth, int len, char * pUgid, int ugidLen ) { lsapi_MD5_CTX md5ctx; unsigned char achMD5[16]; - unsigned char diff = 0; - int i; - if (len != 32 || ugidLen != 8) + if ( len < 32 ) return -1; memmove( achMD5, pAuth + 16, 16 ); memmove( pAuth + 16, s_secret, 16 ); @@ -1168,89 +1099,21 @@ static int lsapi_suexec_auth(char *pAuth, int len, char *pUgid, int ugidLen) lsapi_MD5Update( &md5ctx, (unsigned char *)pAuth, 32 ); lsapi_MD5Update( &md5ctx, (unsigned char *)pUgid, 8 ); lsapi_MD5Final( (unsigned char *)pAuth + 16, &md5ctx); - for (i = 0; i < 16; ++i) - diff |= achMD5[i] ^ (unsigned char)pAuth[i + 16]; - if (diff == 0) + if ( memcmp( achMD5, pAuth + 16, 16 ) == 0 ) return 0; return 1; } -static int lsapi_special_env_is(const struct LSAPI_key_value_pair *pEnv, - const char *name) -{ - size_t len; - if (!pEnv || !pEnv->pKey || !name || pEnv->keyLen < 1) - return 0; - len = strlen(name); - return pEnv->keyLen == (int)len + 1 - && pEnv->pKey[0] == '\0' - && memcmp(pEnv->pKey + 1, name, len) == 0; -} - - -static void lsapi_process_suexec_auth(LSAPI_Request *pReq, - uid_t *uid, gid_t *gid) -{ - struct LSAPI_key_value_pair *pAuth; - struct LSAPI_key_value_pair *pUgid; - uint32_t ugid[2]; - int count; - - if (!pReq || !pReq->m_pHeader || !uid || !gid) - return; - count = pReq->m_pHeader->m_cntSpecialEnv; - if (count < 1 || !pReq->m_pSpecialEnvList) - return; - - pAuth = pReq->m_pSpecialEnvList + count - 1; - if (!lsapi_special_env_is(pAuth, "SUEXEC_AUTH")) - return; - - /* Never expose the authentication record to the application, including - * malformed requests where the matching SUEXEC_UGID record is absent. */ - --pReq->m_pHeader->m_cntSpecialEnv; - if (count < 2) - { - lsapi_log("LSAPI: missing SUEXEC_UGID env, use default user!\n"); - *uid = 0; - goto clear_auth; - } - - pUgid = pAuth - 1; - if (!lsapi_special_env_is(pUgid, "SUEXEC_UGID")) - { - lsapi_log("LSAPI: missing SUEXEC_UGID env, use default user!\n"); - *uid = 0; - goto clear_auth; - } - - --pReq->m_pHeader->m_cntSpecialEnv; - if (lsapi_suexec_auth(pAuth->pValue, pAuth->valLen, - pUgid->pValue, pUgid->valLen) == 0) - { - memcpy(ugid, pUgid->pValue, sizeof(ugid)); - *uid = ugid[0]; - *gid = ugid[1]; - } - else - { - lsapi_log("LSAPI: SUEXEC_AUTH authentication failed, use default user!\n"); - *uid = 0; - } - -clear_auth: - if (pAuth->pValue && pAuth->valLen > 0) - memset(pAuth->pValue, 0, (size_t)pAuth->valLen); -} - - static int lsapi_changeUGid( LSAPI_Request * pReq ) { - uid_t uid = s_defaultUid; - gid_t gid = s_defaultGid; + int uid = s_defaultUid; + int gid = s_defaultGid; const char *pStderrLog; const char *pChroot = NULL; + struct LSAPI_key_value_pair * pEnv; + struct LSAPI_key_value_pair * pAuth; + int i; if ( s_uid ) return 0; //with special ID 0x00 @@ -1258,10 +1121,48 @@ static int lsapi_changeUGid( LSAPI_Request * pReq ) //first one should be MD5( nonce + lscgid secret ) //remember to clear the secret after verification //it should be set at the end of special env - lsapi_process_suexec_auth(pReq, &uid, &gid); + i = pReq->m_pHeader->m_cntSpecialEnv - 1; + if ( i >= 0 ) + { + pEnv = pReq->m_pSpecialEnvList + i; + if (( *pEnv->pKey == '\000' )&& + ( strcmp( pEnv->pKey+1, "SUEXEC_AUTH" ) == 0 )) + { + --pReq->m_pHeader->m_cntSpecialEnv; + pAuth = pEnv--; + if (( *pEnv->pKey == '\000' )&& + ( strcmp( pEnv->pKey+1, "SUEXEC_UGID" ) == 0 )) + { + --pReq->m_pHeader->m_cntSpecialEnv; + uid = *(uint32_t *)pEnv->pValue; + gid = *(((uint32_t *)pEnv->pValue) + 1 ); + //lsapi_log("LSAPI: SUEXEC_UGID set UID: %d, GID: %d\n", uid, gid ); + } + else + { + lsapi_log("LSAPI: missing SUEXEC_UGID env, use default user!\n" ); + pEnv = NULL; + } + if ( pEnv&& lsapi_suexec_auth( pReq, pAuth->pValue, pAuth->valLen, pEnv->pValue, pEnv->valLen ) == 0 ) + { + //read UID, GID from specialEnv + + } + else + { + //authentication error + lsapi_log("LSAPI: SUEXEC_AUTH authentication failed, use default user!\n" ); + uid = 0; + } + } + else + { + //lsapi_log("LSAPI: no SUEXEC_AUTH env, use default user!\n" ); + } + } - if (!uid || !gid) + if ( !uid ) { uid = s_defaultUid; gid = s_defaultGid; @@ -1293,22 +1194,8 @@ static int lsapi_changeUGid( LSAPI_Request * pReq ) static int parseContentLenFromHeader(LSAPI_Request * pReq) { const char * pContentLen = LSAPI_GetHeader_r( pReq, H_CONTENT_LENGTH ); - char *pEnd; - long long len; - /* A missing Content-Length is valid for chunked HTTP/1 requests and for - * HTTP/2 or HTTP/3 streams. Keep the protocol's unknown-length sentinel - * in that case; producers that buffer the body may replace it with the - * decoded length before forwarding the request. */ - if (!pContentLen) - return 0; - - errno = 0; - len = strtoll(pContentLen, &pEnd, 10); - while (*pEnd && isspace((unsigned char)*pEnd)) - ++pEnd; - if (errno == ERANGE || pEnd == pContentLen || *pEnd || len < 0) - return -1; - pReq->m_reqBodyLen = len; + if ( pContentLen ) + pReq->m_reqBodyLen = strtoll( pContentLen, NULL, 10 ); return 0; } @@ -1316,31 +1203,14 @@ static int parseContentLenFromHeader(LSAPI_Request * pReq) static int parseRequest( LSAPI_Request * pReq, int totalLen ) { int shouldFixEndian; - char *pBegin; - char *pEnd; - size_t used; - size_t aligned; - size_t remaining; - size_t unknownSize; - - if (!pReq || !pReq->m_pReqBuf || totalLen < (int)sizeof(struct lsapi_req_header) - || totalLen > pReq->m_reqBufSize) - return -1; - pBegin = pReq->m_pReqBuf + sizeof( struct lsapi_req_header ); - pEnd = pReq->m_pReqBuf + totalLen; + char * pBegin = pReq->m_pReqBuf + sizeof( struct lsapi_req_header ); + char * pEnd = pReq->m_pReqBuf + totalLen; shouldFixEndian = ( LSAPI_ENDIAN != ( pReq->m_pHeader->m_pktHeader.m_flag & LSAPI_ENDIAN_BIT ) ); if ( shouldFixEndian ) { fixEndian( pReq ); } - if (pReq->m_pHeader->m_cntSpecialEnv < 0 - || pReq->m_pHeader->m_cntSpecialEnv > 8192 - || pReq->m_pHeader->m_cntEnv < 0 - || pReq->m_pHeader->m_cntEnv > 8192 - || pReq->m_pHeader->m_cntUnknownHeaders < 0 - || pReq->m_pHeader->m_httpHeaderLen < 0) - return -1; if ( (pReq->m_specialEnvListSize < pReq->m_pHeader->m_cntSpecialEnv )&& allocateEnvList( &pReq->m_pSpecialEnvList, &pReq->m_specialEnvListSize, @@ -1374,46 +1244,21 @@ static int parseRequest( LSAPI_Request * pReq, int totalLen ) pReq->m_pScriptName = pReq->m_pReqBuf + pReq->m_pHeader->m_scriptNameOff; pReq->m_pQueryString = pReq->m_pReqBuf + pReq->m_pHeader->m_queryStringOff; pReq->m_pRequestMethod = pReq->m_pReqBuf + pReq->m_pHeader->m_requestMethodOff; - if (!memchr(pReq->m_pScriptFile, '\0', - (size_t)(pEnd - pReq->m_pScriptFile)) - || !memchr(pReq->m_pScriptName, '\0', - (size_t)(pEnd - pReq->m_pScriptName)) - || !memchr(pReq->m_pQueryString, '\0', - (size_t)(pEnd - pReq->m_pQueryString)) - || !memchr(pReq->m_pRequestMethod, '\0', - (size_t)(pEnd - pReq->m_pRequestMethod))) - return -1; - used = (size_t)(pBegin - pReq->m_pReqBuf); - if (used > (size_t)totalLen || used > SIZE_MAX - 7) - return -1; - aligned = (used + 7) & ~(size_t)7; - if (aligned > (size_t)totalLen) - return -1; - pBegin = pReq->m_pReqBuf + aligned; - remaining = (size_t)(pEnd - pBegin); - if (remaining < sizeof(struct lsapi_http_header_index)) - return -1; + pBegin = pReq->m_pReqBuf + (( pBegin - pReq->m_pReqBuf + 7 ) & (~0x7)); pReq->m_pHeaderIndex = ( struct lsapi_http_header_index * )pBegin; pBegin += sizeof( struct lsapi_http_header_index ); pReq->m_pUnknownHeader = (struct lsapi_header_offset *)pBegin; - remaining = (size_t)(pEnd - pBegin); - if ((size_t)pReq->m_pHeader->m_cntUnknownHeaders - > remaining / sizeof(struct lsapi_header_offset)) - return -1; - unknownSize = sizeof(struct lsapi_header_offset) - * (size_t)pReq->m_pHeader->m_cntUnknownHeaders; - pBegin += unknownSize; + pBegin += sizeof( struct lsapi_header_offset) * + pReq->m_pHeader->m_cntUnknownHeaders; pReq->m_pHttpHeader = pBegin; - remaining = (size_t)(pEnd - pBegin); - if ((size_t)pReq->m_pHeader->m_httpHeaderLen != remaining) + pBegin += pReq->m_pHeader->m_httpHeaderLen; + if ( pBegin != pEnd ) { - lsapi_log("Request header does not match total size, total: %d, " - "real: %ld\n", totalLen, - pBegin - pReq->m_pReqBuf - + pReq->m_pHeader->m_httpHeaderLen ); + lsapi_log("Request header does match total size, total: %d, " + "real: %ld\n", totalLen, pBegin - pReq->m_pReqBuf ); return -1; } if ( shouldFixEndian ) @@ -1430,8 +1275,7 @@ static int parseRequest( LSAPI_Request * pReq, int totalLen ) pReq->m_reqBodyLen = pReq->m_pHeader->m_reqBodyLen; if ( pReq->m_reqBodyLen == -2 ) { - if (parseContentLenFromHeader(pReq) == -1) - return -1; + parseContentLenFromHeader(pReq); } return 0; @@ -1528,8 +1372,7 @@ static int readReq( LSAPI_Request * pReq ) while ( pReq->m_bufRead < LSAPI_PACKET_HEADER_LEN ) { - len = lsapi_read(pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, - pReq->m_reqBufSize - pReq->m_bufRead); + len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf, pReq->m_reqBufSize ); if ( len <= 0 ) return -1; pReq->m_bufRead += len; @@ -1542,11 +1385,6 @@ static int readReq( LSAPI_Request * pReq ) lsapi_log("packetLen < 0\n"); return -1; } - if (packetLen < (int)sizeof(struct lsapi_req_header)) - { - lsapi_log("packetLen < request header size\n"); - return -1; - } if ( packetLen > LSAPI_MAX_HEADER_LEN ) { lsapi_log("packetLen > %d\n", LSAPI_MAX_HEADER_LEN ); @@ -1638,16 +1476,6 @@ void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf cb) } -void LSAPI_Set_Fork_Hooks(LSAPI_Fork_Hook_pf before, - LSAPI_Fork_Hook_pf after_parent, - LSAPI_Fork_Hook_pf after_child) -{ - s_fork_before = before; - s_fork_after_parent = after_parent; - s_fork_after_child = after_child; -} - - int LSAPI_InitRequest( LSAPI_Request * pReq, int fd ) { int newfd; @@ -1655,44 +1483,21 @@ int LSAPI_InitRequest( LSAPI_Request * pReq, int fd ) return -1; memset( pReq, 0, sizeof( LSAPI_Request ) ); if ( allocateIovec( pReq, 16 ) == -1 ) - { - LSAPI_Release_r(pReq); return -1; - } pReq->m_pRespBuf = pReq->m_pRespBufPos = (char *)malloc( LSAPI_RESP_BUF_SIZE ); if ( !pReq->m_pRespBuf ) - { - LSAPI_Release_r(pReq); return -1; - } pReq->m_pRespBufEnd = pReq->m_pRespBuf + LSAPI_RESP_BUF_SIZE; pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec + 1; pReq->m_respPktHeaderEnd = &pReq->m_respPktHeader[5]; if ( allocateRespHeaderBuf( pReq, LSAPI_INIT_RESP_HEADER_LEN ) == -1 ) - { - LSAPI_Release_r(pReq); return -1; - } if ( fd == STDIN_FILENO ) { fd = dup( fd ); - if (fd == -1) - { - LSAPI_Release_r(pReq); - return -1; - } newfd = open( "/dev/null", O_RDWR ); - if (newfd == -1 || dup2(newfd, STDIN_FILENO) == -1) - { - if (newfd != -1) - close(newfd); - close(fd); - LSAPI_Release_r(pReq); - return -1; - } - if (newfd != STDIN_FILENO) - close(newfd); + dup2( newfd, STDIN_FILENO ); } if ( isPipe( fd ) ) @@ -1867,8 +1672,6 @@ void LSAPI_Reset_r( LSAPI_Request * pReq ) int LSAPI_Release_r( LSAPI_Request * pReq ) { - if (!pReq) - return -1; if ( pReq->m_pReqBuf ) free( pReq->m_pReqBuf ); if ( pReq->m_pSpecialEnvList ) @@ -1877,13 +1680,6 @@ int LSAPI_Release_r( LSAPI_Request * pReq ) free( pReq->m_pEnvList ); if ( pReq->m_pRespHeaderBuf ) free( pReq->m_pRespHeaderBuf ); - if ( pReq->m_pRespBuf ) - free( pReq->m_pRespBuf ); - if ( pReq->m_pIovec ) - free( pReq->m_pIovec ); - memset(pReq, 0, sizeof(*pReq)); - pReq->m_fdListen = -1; - pReq->m_fd = -1; return 0; } @@ -1910,13 +1706,11 @@ static int readBodyToReqBuf( LSAPI_Request * pReq ) { off_t bodyLeft; ssize_t len = pReq->m_bufRead - pReq->m_bufProcessed; - bodyLeft = pReq->m_reqBodyLen - pReq->m_reqBodyRead; - if (bodyLeft <= 0) - return 0; if ( len > 0 ) - return len > bodyLeft ? bodyLeft : len; + return len; pReq->m_bufRead = pReq->m_bufProcessed = pReq->m_pHeader->m_pktHeader.m_packetLen.m_iLen; + bodyLeft = pReq->m_reqBodyLen - pReq->m_reqBodyRead; len = pReq->m_reqBufSize - pReq->m_bufRead; if ( len < 0 ) return -1; @@ -1934,8 +1728,6 @@ int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq ) { if (!pReq || (pReq->m_fd ==-1) ) return EOF; - if (pReq->m_reqBodyRead >= pReq->m_reqBodyLen) - return EOF; if ( pReq->m_bufProcessed >= pReq->m_bufRead ) { if ( readBodyToReqBuf( pReq ) <= 0 ) @@ -1954,18 +1746,20 @@ int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen, in char * pBufCur = pBuf; char * pCur; char * p; - if (!pReq || pReq->m_fd == -1 || !pBuf || !getLF || bufLen == 0 - || bufLen > (size_t)SSIZE_MAX) + if (!pReq || pReq->m_fd == -1 || !pBuf || !getLF) return -1; *getLF = 0; while( (left = pBufEnd - pBufCur ) > 0 ) { - len = readBodyToReqBuf(pReq); - if (len <= 0) + len = pReq->m_bufRead - pReq->m_bufProcessed; + if ( len <= 0 ) { - *getLF = 1; - break; + if ( (len = readBodyToReqBuf( pReq )) <= 0 ) + { + *getLF = 1; + break; + } } if ( len > left ) len = left; @@ -1996,7 +1790,7 @@ ssize_t LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen ) ssize_t len; off_t total; /* char *pOldBuf = pBuf; */ - if (!pReq || pReq->m_fd == -1 || !pBuf || bufLen > (size_t)SSIZE_MAX) + if (!pReq || pReq->m_fd == -1 || !pBuf || (ssize_t)bufLen < 0) return -1; total = pReq->m_reqBodyLen - pReq->m_reqBodyRead; @@ -2050,7 +1844,7 @@ ssize_t LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, size_t len ) ssize_t packetLen; int skip = 0; - if (!pReq || !pBuf || len > (size_t)SSIZE_MAX) + if (!pReq || !pBuf) return -1; if (pReq->m_reqState & LSAPI_ST_BACKGROUND) return len; @@ -2158,25 +1952,24 @@ ssize_t gsendfile( int fdOut, int fdIn, off_t* off, size_t size ) } while (size > 0) { - size_t tor = size > sizeof(in) ? sizeof(in) : size; - ssize_t c = read(fdIn, in, tor); - if (c <= 0) { - goto end; - } + size_t tor = size > sizeof(in) ? sizeof(in) : size; + ssize_t c = read(fdIn, in, tor); + if (c <= 0) { + goto end; + } - ssize_t w = write(fdOut, in, c); - if (w > 0) - written += w; + ssize_t w = write(fdOut, in, c); + if (w != c) { + goto end; + } - if (w != c) { - goto end; - } - size -= c; + written += w; + size -= c; } end: *off += written; - return written; + return 0; } #endif @@ -2187,7 +1980,7 @@ ssize_t gsendfile( int fdOut, int fdIn, off_t* off, size_t size ) ssize_t ret; off_t len = size; ret = sendfile( fdIn, fdOut, *off, &len, NULL, 0 ); - if (len > 0) + if (( ret == 0 )&&( len > 0 )) { ret = len; *off += len; @@ -2235,120 +2028,28 @@ ssize_t gsendfile( int fdOut, int fdIn, off_t * off, size_t size ) } #endif -#ifndef LSAPI_GSENDFILE -#define LSAPI_GSENDFILE gsendfile -#endif - - -static int lsapi_sendfile_write_header(LSAPI_Request *pReq, - const struct lsapi_packet_header *header) -{ - const char *p = (const char *)header; - size_t left = LSAPI_PACKET_HEADER_LEN; - - while (left > 0 && g_running) - { - ssize_t written = write(pReq->m_fd, p, left); - if (written > 0) - { - p += written; - left -= (size_t)written; - } - else if (written == -1 && errno == EINTR) - continue; - else - break; - } - if (left == 0) - return 0; - lsapi_close_connection(pReq); - return -1; -} - ssize_t LSAPI_sendfile_r( LSAPI_Request * pReq, int fdIn, off_t* off, size_t size ) { - struct lsapi_packet_header *pHeader; - struct stat st; - size_t remaining; - size_t totalSent = 0; - - if (!pReq || pReq->m_fd == -1 || fdIn == -1 || !off || *off < 0 - || size > (size_t)SSIZE_MAX) + struct lsapi_packet_header * pHeader = pReq->m_respPktHeader; + if ( !pReq || (pReq->m_fd == -1) || fdIn == -1 ) return -1; - if (fstat(fdIn, &st) == 0 && S_ISREG(st.st_mode)) - { - uintmax_t available; - if (*off >= st.st_size) - return 0; - available = (uintmax_t)(st.st_size - *off); - if ((uintmax_t)size > available) - size = (size_t)available; - } - if (size == 0) - return 0; - - pHeader = pReq->m_respPktHeader; if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER ) { - if (LSAPI_FinalizeRespHeaders_r(pReq) == -1) - return -1; + LSAPI_FinalizeRespHeaders_r( pReq ); } pReq->m_reqState |= LSAPI_ST_RESP_BODY; - if (LSAPI_Flush_r(pReq) == -1) - return -1; + LSAPI_Flush_r(pReq); - remaining = size; - while (remaining > 0) - { - size_t packetLeft = remaining; - if (!g_running) - { - lsapi_close_connection(pReq); - return -1; - } - if (packetLeft > LSAPI_MAX_DATA_PACKET_LEN) - packetLeft = LSAPI_MAX_DATA_PACKET_LEN; - lsapi_buildPacketHeader(pHeader, LSAPI_RESP_STREAM, - (int)packetLeft + LSAPI_PACKET_HEADER_LEN); - if (lsapi_sendfile_write_header(pReq, pHeader) == -1) - return -1; + lsapi_buildPacketHeader( pHeader, LSAPI_RESP_STREAM, + size + LSAPI_PACKET_HEADER_LEN ); - while (packetLeft > 0) - { - ssize_t sent; - if (!g_running) - { - lsapi_close_connection(pReq); - return -1; - } - sent = LSAPI_GSENDFILE(pReq->m_fd, fdIn, off, packetLeft); - if (sent > 0 && (size_t)sent <= packetLeft) - { - packetLeft -= (size_t)sent; - remaining -= (size_t)sent; - totalSent += (size_t)sent; - } - else if (sent == -1 && errno == EINTR) - { - if (!g_running) - { - lsapi_close_connection(pReq); - return -1; - } - continue; - } - else - { - /* Once a packet header has been written, the connection must - * not be reused unless its complete payload is delivered. */ - lsapi_close_connection(pReq); - return -1; - } - } - } - return (ssize_t)totalSent; + + if (write(pReq->m_fd, (const char *) pHeader, LSAPI_PACKET_HEADER_LEN ) != LSAPI_PACKET_HEADER_LEN) + return -1; + + return gsendfile( pReq->m_fd, fdIn, off, size ); } @@ -2430,7 +2131,7 @@ ssize_t LSAPI_Write_Stderr_r( LSAPI_Request * pReq, const char * pBuf, size_t le struct iovec iov[2]; struct iovec *pIov; - if (!pReq || !pBuf || len > (size_t)SSIZE_MAX) + if ( !pReq ) return -1; if (s_stderr_log_path || pReq->m_fd == -1 || pReq->m_fd == pReq->m_fdListen) return write( 2, pBuf, len ); @@ -2510,7 +2211,7 @@ static char * GetHeaderVar( LSAPI_Request * pReq, const char * name ) while(( pKey < pKeyEnd )&&( *p )) { - char ch = toupper((unsigned char)*pKey); + char ch = toupper( (unsigned char)*pKey ); if ((ch != *p )||(( *p == '_' )&&( ch != '-'))) break; ++p; ++pKey; @@ -2534,19 +2235,10 @@ static char * GetHeaderVar( LSAPI_Request * pReq, const char * name ) char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name ) { - struct LSAPI_key_value_pair *pBegin; - struct LSAPI_key_value_pair *pEnd; + struct LSAPI_key_value_pair * pBegin = pReq->m_pEnvList; + struct LSAPI_key_value_pair * pEnd = pBegin + pReq->m_pHeader->m_cntEnv; if ( !pReq || !name ) return NULL; - if (!pReq->m_pHeader || pReq->m_pHeader->m_cntEnv <= 0 - || !pReq->m_pEnvList) - { - if (strncmp(name, "HTTP_", 5) == 0 && pReq->m_pHeaderIndex) - return GetHeaderVar(pReq, name); - return NULL; - } - pBegin = pReq->m_pEnvList; - pEnd = pBegin + pReq->m_pHeader->m_cntEnv; if ( strncmp( name, "HTTP_", 5 ) == 0 ) { return GetHeaderVar( pReq, name ); @@ -2572,9 +2264,8 @@ struct _headerInfo int compareValueLocation(const void * v1, const void *v2 ) { - const char *p1 = ((const struct _headerInfo *)v1)->_value; - const char *p2 = ((const struct _headerInfo *)v2)->_value; - return (p1 > p2) - (p1 < p2); + return ((const struct _headerInfo *)v1)->_value - + ((const struct _headerInfo *)v2)->_value; } @@ -2704,7 +2395,7 @@ int LSAPI_ForeachHeader_r( LSAPI_Request * pReq, if ( ch == '-' ) *p++ = '_'; else - *p++ = toupper((unsigned char)ch); + *p++ = toupper( (unsigned char)ch ); } *p = 0; keyLen += 5; @@ -2801,8 +2492,7 @@ int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq ) int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName, const char * pHeaderValue ) { - size_t nameLen, valLen; - int len; + int nameLen, valLen, len; if ( !pReq || !pHeaderName || !pHeaderValue ) return -1; if ( pReq->m_reqState & LSAPI_ST_RESP_BODY ) @@ -2811,8 +2501,7 @@ int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName, return -1; nameLen = strlen( pHeaderName ); valLen = strlen( pHeaderValue ); - if (nameLen == 0 || nameLen > LSAPI_RESP_HTTP_HEADER_MAX - || valLen > LSAPI_RESP_HTTP_HEADER_MAX) + if ( nameLen == 0 ) return -1; while( nameLen > 0 ) { @@ -2832,14 +2521,9 @@ int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName, else break; } - if (memchr(pHeaderName, '\r', nameLen) || memchr(pHeaderName, '\n', nameLen) - || memchr(pHeaderName, ':', nameLen) - || memchr(pHeaderValue, '\r', valLen) - || memchr(pHeaderValue, '\n', valLen)) + len = nameLen + valLen + 1; + if ( len > LSAPI_RESP_HTTP_HEADER_MAX ) return -1; - if (nameLen + valLen + 2 > SHRT_MAX) - return -1; - len = (int)(nameLen + valLen + 1); if ( pReq->m_pRespHeaderBufPos + len + 1 > pReq->m_pRespHeaderBufEnd ) { @@ -2879,9 +2563,6 @@ int LSAPI_AppendRespHeader_r( LSAPI_Request * pReq, const char * pBuf, int len ) } if ( len <= 0 ) return 0; - if (len >= SHRT_MAX || memchr(pBuf, '\0', len) - || memchr(pBuf, '\r', len) || memchr(pBuf, '\n', len)) - return -1; if ( pReq->m_pRespHeaderBufPos + len + 1 > pReq->m_pRespHeaderBufEnd ) { int newlen = pReq->m_pRespHeaderBufPos + len + 4096 - pReq->m_pRespHeaderBuf; @@ -2905,42 +2586,19 @@ int LSAPI_CreateListenSock2( const struct sockaddr * pServerAddr, int backlog ) int fd; int flag = 1; int addr_len; - struct stat st; - - if (!pServerAddr) - return -1; switch( pServerAddr->sa_family ) { case AF_INET: - addr_len = sizeof(struct sockaddr_in); + addr_len = 16; break; case AF_INET6: addr_len = sizeof( struct sockaddr_in6 ); break; case AF_UNIX: - { - const char *path = ((const struct sockaddr_un *)pServerAddr)->sun_path; - size_t pathLen = strnlen(path, - sizeof(((const struct sockaddr_un *)pServerAddr)->sun_path)); - if (pathLen == 0 - || pathLen == sizeof(((const struct sockaddr_un *)pServerAddr)->sun_path)) - return -1; - addr_len = offsetof(struct sockaddr_un, sun_path) + pathLen + 1; - if (lstat(path, &st) == 0) - { - if (!S_ISSOCK(st.st_mode)) - { - errno = EEXIST; - return -1; - } - if (unlink(path) == -1) - return -1; - } - else if (errno != ENOENT) - return -1; + addr_len = sizeof( struct sockaddr_un ); + unlink( ((struct sockaddr_un *)pServerAddr)->sun_path ); break; - } default: return -1; } @@ -2979,28 +2637,22 @@ int LSAPI_ParseSockAddr( const char * pBind, struct sockaddr * pAddr ) int doAddrInfo = 0; int port; - if ( !pBind || !pAddr ) + if ( !pBind ) return -1; - while (isspace((unsigned char)*pBind)) + while(isspace( (unsigned char)*pBind ) ) ++pBind; - if (strlen(pBind) >= sizeof(achAddr)) - return -1; - strcpy(achAddr, pBind); + strncpy(achAddr, pBind, 255); + achAddr[255] = 0; switch( *p ) { case '/': - { - size_t pathLen = strlen(p); - if (pathLen >= sizeof(((struct sockaddr_un *)pAddr)->sun_path)) - return -1; pAddr->sa_family = AF_UNIX; memccpy(((struct sockaddr_un *)pAddr)->sun_path, p, 0, sizeof(((struct sockaddr_un *)pAddr)->sun_path)); return 0; - } case '[': pAddr->sa_family = AF_INET6; @@ -3256,6 +2908,7 @@ static void lsapi_sigchild( int signal ) if ( WIFSIGNALED( status )) { int sig_num = WTERMSIG( status ); + #ifdef WCOREDUMP const char * dump = WCOREDUMP( status ) ? "yes" : "no"; #else @@ -3358,14 +3011,12 @@ static void dump_debug_info( lsapi_child_status * pStatus, long tmCur ) s_pid_dump_debug_info = fork(); if (s_pid_dump_debug_info == 0) { - snprintf(achCmd, sizeof(achCmd), - "PATH=/usr/bin:/bin:/usr/sbin:/sbin; export PATH; " - "gdb --batch -ex \"attach %d\" -ex \"set height 0\" " - "-ex \"bt\" >&2; lsof -p %d >&2", + snprintf( achCmd, 1024, "gdb --batch -ex \"attach %d\" -ex \"set height 0\" " + "-ex \"bt\" >&2;PATH=$PATH:/usr/sbin lsof -p %d >&2", pStatus->m_pid, pStatus->m_pid ); if ( system( achCmd ) == -1 ) perror( "system()" ); - _exit( 0 ); + exit( 0 ); } } @@ -3618,8 +3269,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer, perror( "sigprocmask(SIG_BLOCK) to block SIGCHLD" ); } - if (s_fork_before) - (*s_fork_before)(); pid = fork(); if ( !pid ) @@ -3627,8 +3276,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer, setsid(); if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) perror( "sigprocmask( SIG_SETMASK ) to restore SIGMASK in child" ); - if (s_fork_after_child) - (*s_fork_after_child)(); g_prefork_server = NULL; s_ppid = getppid(); s_pid = getpid(); @@ -3669,17 +3316,12 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer, } else if ( pid == -1 ) { - int fork_errno = errno; - if (s_fork_after_parent) - (*s_fork_after_parent)(); - lsapi_perror("fork() failed, please increase process limit", fork_errno); + lsapi_perror("fork() failed, please increase process limit", errno); if (child_status) child_status->m_pid = 0; } else { - if (s_fork_after_parent) - (*s_fork_after_parent)(); ++pServer->m_iCurChildren; if ( child_status ) { @@ -3779,8 +3421,6 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq) struct timeval timeout; int wait_secs = 0; int ret = 0; - int accepted = 0; - sigset_t chld_mask, orig_mask; lsapi_prefork_server * pServer = g_prefork_server; @@ -3796,13 +3436,6 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq) perror( "Can't set signal handler for SIGCHILD" ); return -1; } - /* The caller forks between calls, with the caller's SIGCHLD handler in - * place; reap workers that exited meanwhile, or they stay counted. */ - sigemptyset(&chld_mask); - sigaddset(&chld_mask, SIGCHLD); - sigprocmask(SIG_BLOCK, &chld_mask, &orig_mask); - lsapi_sigchild(SIGCHLD); - sigprocmask(SIG_SETMASK, &orig_mask, NULL); /* Set up handler to kill children upon exit */ act.sa_flags = 0; @@ -3819,9 +3452,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq) s_stop = 0; pReq->m_reqState = 0; - /* A stop signal that arrives between calls (while the caller forks) is - * handled by lsapi_siguser1(), which only clears g_running. */ - while(!s_stop && g_running) + while(!s_stop) { if (s_proc_group_timer_cb != NULL) { s_proc_group_timer_cb(&s_ignore_pid); @@ -3903,7 +3534,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq) wait_secs = 0; pReq->child_status = find_child_status(0); - accepted = 1; + ret = 0; break; } else @@ -3922,7 +3553,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq) sigaction(SIGINT, &old_int, 0); sigaction(SIGUSR1, &old_usr1, 0); - return accepted ? 0 : -1; + return ret; } @@ -4250,70 +3881,34 @@ static int lsapi_initSuEXEC(void) s_defaultGid = 10000; } } - if (!s_defaultUid || !s_defaultGid) - return -1; return 0; } -static int lsapi_path_is_forbidden(const char *path) -{ - return strcmp(path, "/etc") == 0 || strncmp(path, "/etc/", 5) == 0; -} - - static int lsapi_check_path(const char *p, char *final, int max_len) { char resolved_path[PATH_MAX+1]; - char parent_path[PATH_MAX+1]; - size_t len = 0; - size_t inputLen; - char *slash; - - if (!p || !*p || !final || max_len <= 1) - { - errno = EINVAL; - return -1; - } - inputLen = strlen(p); + int len = 0; + char *end; if (*p != '/') { if (getcwd(final, max_len) == NULL) return -1; len = strlen(final); - if (len + 1 >= (size_t)max_len) - { - errno = ENAMETOOLONG; - return -1; - } - final[len++] = '/'; + *(final + len) = '/'; + ++len; } - if (inputLen >= (size_t)max_len - len) + end = memccpy(&final[len], p, '\0', PATH_MAX - len); + if (!end) { - errno = ENAMETOOLONG; + errno = EINVAL; return -1; } - memcpy(final + len, p, inputLen + 1); - - if (realpath(final, resolved_path) == NULL) - { - if (errno != ENOENT) - return -1; - memcpy(parent_path, final, strlen(final) + 1); - slash = strrchr(parent_path, '/'); - if (!slash) - { - errno = EINVAL; - return -1; - } - if (slash == parent_path) - slash[1] = '\0'; - else - *slash = '\0'; - if (realpath(parent_path, resolved_path) == NULL) - return -1; - } - if (lsapi_path_is_forbidden(resolved_path)) + p = final; + if (realpath(p, resolved_path) == NULL + && errno != ENOENT && errno != EACCES) + return -1; + if (strncmp(resolved_path, "/etc/", 5) == 0) { errno = EPERM; return -1; @@ -4324,14 +3919,7 @@ static int lsapi_check_path(const char *p, char *final, int max_len) static int lsapi_reopen_stderr2(const char *full_path) { - int flags = O_WRONLY | O_CREAT | O_APPEND; -#ifdef O_CLOEXEC - flags |= O_CLOEXEC; -#endif -#ifdef O_NOFOLLOW - flags |= O_NOFOLLOW; -#endif - int newfd = open(full_path, flags, 0644); + int newfd = open(full_path, O_WRONLY | O_CREAT | O_APPEND, 0644); if (newfd == -1) { LSAPI_perror_r(NULL, "Failed to open custom stderr log", full_path); @@ -4339,15 +3927,10 @@ static int lsapi_reopen_stderr2(const char *full_path) } if (newfd != 2) { - if (dup2(newfd, 2) == -1) - { - close(newfd); - return -1; - } + dup2(newfd, 2); close(newfd); + dup2(2, 1); } - if (dup2(2, 1) == -1) - return -1; if (s_stderr_log_path && full_path != s_stderr_log_path) { free(s_stderr_log_path); @@ -4507,14 +4090,7 @@ int LSAPI_Init_Env_Parameters( fn_select_t fp ) if ( lsapi_initSuEXEC() == -1 ) return -1; #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__) - if (lsapi_initLVE() == -1) - { - /* Some historical callers ignored this function's return value. - * Stop the accept loop as well so configured LVE enforcement can - * never degrade silently. */ - g_running = 0; - return -1; - } + lsapi_initLVE(); #endif } unset_lsapi_envs(); diff --git a/sapi/litespeed/lsapilib.h b/sapi/litespeed/lsapilib.h index 1165938b61c4..922114b73be3 100644 --- a/sapi/litespeed/lsapilib.h +++ b/sapi/litespeed/lsapilib.h @@ -318,11 +318,7 @@ static inline int LSAPI_ReqBodyGetChar(void) { return LSAPI_ReqBodyGetChar_r( &g_req ); } static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF ) -{ - if (len <= 0) - return -1; - return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, (size_t)len, getLF ); -} +{ return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF ); } @@ -330,11 +326,7 @@ static inline int LSAPI_FinalizeRespHeaders(void) { return LSAPI_FinalizeRespHeaders_r( &g_req ); } static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len ) -{ - if (len < 0) - return -1; - return LSAPI_Write_r( &g_req, pBuf, (size_t)len ); -} +{ return LSAPI_Write_r( &g_req, pBuf, len ); } static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size ) { @@ -342,11 +334,7 @@ static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size ) } static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len ) -{ - if (len < 0) - return -1; - return LSAPI_Write_Stderr_r( &g_req, pBuf, (size_t)len ); -} +{ return LSAPI_Write_Stderr_r( &g_req, pBuf, len ); } static inline int LSAPI_Flush(void) { return LSAPI_Flush_r( &g_req ); } @@ -402,14 +390,6 @@ int LSAPI_Set_Restored_Parent_Pid(int pid); typedef void (*LSAPI_On_Timer_pf)(int *forked_child_pid); void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf); -/* Called around the fork() of each worker by the prefork server - * (LSAPI_Prefork_Accept_r): before in the parent, then after_parent in the - * parent (also when fork() fails) or after_child in the new worker. */ -typedef void (*LSAPI_Fork_Hook_pf)(void); -void LSAPI_Set_Fork_Hooks(LSAPI_Fork_Hook_pf before, - LSAPI_Fork_Hook_pf after_parent, - LSAPI_Fork_Hook_pf after_child); - int LSAPI_Inc_Req_Processed(int cnt); int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq);

« previous php.cvs (#143115) next »