lib: use wrapper for curl_mime_data fseek callback

fseek uses long offset which does not match with curl_off_t. This leads
to undefined behavior when calling the callback and caused failure on
arm 32 bit.

Use a wrapper to solve this and use fseeko which uses off_t instead of
long.

Thanks to the nice people at Libera IRC #musl for helping finding this
out.

Fixes #11882
Fixes #11900
Closes #11918
This commit is contained in:
Natanael Copa 2023-09-22 13:58:49 +00:00 committed by Daniel Stenberg
parent b226bd679a
commit 9c7165e96a
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
3 changed files with 20 additions and 2 deletions

View File

@ -1037,6 +1037,7 @@ check_include_file_concat("signal.h" HAVE_SIGNAL_H)
check_include_file_concat("stdatomic.h" HAVE_STDATOMIC_H) check_include_file_concat("stdatomic.h" HAVE_STDATOMIC_H)
check_include_file_concat("stdbool.h" HAVE_STDBOOL_H) check_include_file_concat("stdbool.h" HAVE_STDBOOL_H)
check_include_file_concat("stdint.h" HAVE_STDINT_H) check_include_file_concat("stdint.h" HAVE_STDINT_H)
check_include_file_concat("stdio.h" HAVE_STDIO_H)
check_include_file_concat("stdlib.h" HAVE_STDLIB_H) check_include_file_concat("stdlib.h" HAVE_STDLIB_H)
check_include_file_concat("string.h" HAVE_STRING_H) check_include_file_concat("string.h" HAVE_STRING_H)
check_include_file_concat("strings.h" HAVE_STRINGS_H) check_include_file_concat("strings.h" HAVE_STRINGS_H)
@ -1122,6 +1123,8 @@ endif()
check_symbol_exists(freeaddrinfo "${CURL_INCLUDES}" HAVE_FREEADDRINFO) check_symbol_exists(freeaddrinfo "${CURL_INCLUDES}" HAVE_FREEADDRINFO)
check_symbol_exists(pipe "${CURL_INCLUDES}" HAVE_PIPE) check_symbol_exists(pipe "${CURL_INCLUDES}" HAVE_PIPE)
check_symbol_exists(ftruncate "${CURL_INCLUDES}" HAVE_FTRUNCATE) check_symbol_exists(ftruncate "${CURL_INCLUDES}" HAVE_FTRUNCATE)
check_symbol_exists(fseeko "${CURL_INCLUDES}" HAVE_FSEEKO)
check_symbol_exists(_fseeki64 "${CURL_INCLUDES}" HAVE__FSEEKI64)
check_symbol_exists(getpeername "${CURL_INCLUDES}" HAVE_GETPEERNAME) check_symbol_exists(getpeername "${CURL_INCLUDES}" HAVE_GETPEERNAME)
check_symbol_exists(getsockname "${CURL_INCLUDES}" HAVE_GETSOCKNAME) check_symbol_exists(getsockname "${CURL_INCLUDES}" HAVE_GETSOCKNAME)
check_symbol_exists(if_nametoindex "${CURL_INCLUDES}" HAVE_IF_NAMETOINDEX) check_symbol_exists(if_nametoindex "${CURL_INCLUDES}" HAVE_IF_NAMETOINDEX)

View File

@ -3584,10 +3584,12 @@ AC_CHECK_DECLS([getpwuid_r], [], [AC_DEFINE(HAVE_DECL_GETPWUID_R_MISSING, 1, "Se
AC_CHECK_FUNCS([\ AC_CHECK_FUNCS([\
_fseeki64 \
arc4random \ arc4random \
fchmod \ fchmod \
fnmatch \ fnmatch \
fork \ fork \
fseeko \
geteuid \ geteuid \
getpass_r \ getpass_r \
getppid \ getppid \

View File

@ -789,6 +789,20 @@ static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
return res; return res;
} }
/* wrap call to fseeko so it matches the calling convetion of callback */
static int fseeko_wrapper(void *stream, curl_off_t offset, int whence)
{
#if defined(HAVE_FSEEKO)
return fseeko(stream, (off_t)offset, whence);
#elif defined(HAVE__FSEEKI64)
return _fseeki64(stream, (__int64)offset, whence);
#else
if(offset > LONG_MAX)
return -1;
return fseek(stream, (long)offset, whence);
#endif
}
/* /*
* Curl_getformdata() converts a linked list of "meta data" into a mime * Curl_getformdata() converts a linked list of "meta data" into a mime
* structure. The input list is in 'post', while the output is stored in * structure. The input list is in 'post', while the output is stored in
@ -874,8 +888,7 @@ CURLcode Curl_getformdata(struct Curl_easy *data,
compatibility: use of "-" pseudo file name should be avoided. */ compatibility: use of "-" pseudo file name should be avoided. */
result = curl_mime_data_cb(part, (curl_off_t) -1, result = curl_mime_data_cb(part, (curl_off_t) -1,
(curl_read_callback) fread, (curl_read_callback) fread,
CURLX_FUNCTION_CAST(curl_seek_callback, fseeko_wrapper,
fseek),
NULL, (void *) stdin); NULL, (void *) stdin);
} }
else else