[security][CVE-2022-27809] Builtins should always take int64_t, not intnightly-2022.04.05
commitd130ec6375da61ebb444e3cad61ab3da0b031276
authorYang, Bo <pop.atry@gmail.com>
Fri, 25 Mar 2022 18:49:25 +0000 (25 11:49 -0700)
committerYang, Bo <pop.atry@gmail.com>
Mon, 4 Apr 2022 23:13:15 +0000 (4 16:13 -0700)
treecd3fa48dcbdde37dd54326ffce9d164d2c5c83fc
parent3f8983a477de80a75c79fd53a09aaeca300df0d3
[security][CVE-2022-27809] Builtins should always take int64_t, not int

Builtins are allowed to take 32 or 64-bit integers
(int/int32_t versus int64_t). It's not clear why we allow this, as
integers from Hack are always 64-bit. A builtin taking an int will
just have the upper 32-bits truncated. Depending on the value, this
might change a negative number to a positive one, defeating various
input validity checks.
Remove support for 32-bit integers. Change all of the builtins which
took or returned an int to use int64_t instead. This required a few
fixups here and there, mainly around format strings. If the builtin
uses 32-bits ints internally, I left them. At least the truncation is
now obvious.

Test Plan:
New unit test. Beforehand this large negative input would
be turned into a large positive number, bypassing the "input cannot be
negative" check. Now that it takes int64_t, it is correctly recognized
as a negative number.

---
Initializes m_arr in ArrayInitBase

ArrayInitBase's protected constructor does not initialize
m_arr (presumably because a derived class is meant to do so). However
if a derived class' constructor throws, we'll run the ArrayInitBase
destructor, which will try to release m_arr. If m_arr hasn't been
initialized yet, it can contain garbage and we'll try to release
random memory. The protected constructor should initialize m_arr to
nullptr.

Test Plan:
New unit test which previously failed with ASAN builds

Co-authored-by: Rick Lavoie <rlavoie@fb.com>
74 files changed:
hphp/runtime/base/array-init.h
hphp/runtime/ext/array/ext_array.cpp
hphp/runtime/ext/array/ext_array.h
hphp/runtime/ext/asio/ext_asio.cpp
hphp/runtime/ext/async_mysql/ext_async_mysql.cpp
hphp/runtime/ext/bz2/ext_bz2.cpp
hphp/runtime/ext/curl/ext_curl.cpp
hphp/runtime/ext/curl/ext_curl.h
hphp/runtime/ext/datetime/ext_datetime.cpp
hphp/runtime/ext/datetime/ext_datetime.h
hphp/runtime/ext/gd/ext_gd.cpp
hphp/runtime/ext/hotprofiler/ext_hotprofiler.cpp
hphp/runtime/ext/imagick/imagick.cpp
hphp/runtime/ext/imagick/imagickdraw.cpp
hphp/runtime/ext/imagick/imagickpixeliterator.cpp
hphp/runtime/ext/ldap/ext_ldap.cpp
hphp/runtime/ext/ldap/ext_ldap.h
hphp/runtime/ext/mbstring/ext_mbstring.cpp
hphp/runtime/ext/mbstring/ext_mbstring.h
hphp/runtime/ext/mcrypt/ext_mcrypt.cpp
hphp/runtime/ext/memcache/ext_memcache.cpp
hphp/runtime/ext/memcached/ext_memcached.cpp
hphp/runtime/ext/mysql/ext_mysql.cpp
hphp/runtime/ext/objprof/ext_objprof.cpp
hphp/runtime/ext/openssl/ext_openssl.cpp
hphp/runtime/ext/openssl/ext_openssl.h
hphp/runtime/ext/pcre/ext_pcre.cpp
hphp/runtime/ext/pcre/ext_pcre.h
hphp/runtime/ext/pgsql/pgsql.cpp
hphp/runtime/ext/posix/ext_posix.cpp
hphp/runtime/ext/posix/ext_posix.h
hphp/runtime/ext/process/ext_process.cpp
hphp/runtime/ext/process/ext_process.h
hphp/runtime/ext/reflection/ext_reflection.cpp
hphp/runtime/ext/sockets/ext_sockets.cpp
hphp/runtime/ext/sockets/ext_sockets.h
hphp/runtime/ext/sodium/ext_sodium.cpp
hphp/runtime/ext/std/ext_std_errorfunc.cpp
hphp/runtime/ext/std/ext_std_errorfunc.h
hphp/runtime/ext/std/ext_std_file.cpp
hphp/runtime/ext/std/ext_std_file.h
hphp/runtime/ext/std/ext_std_intrinsics.cpp
hphp/runtime/ext/std/ext_std_misc.cpp
hphp/runtime/ext/std/ext_std_misc.h
hphp/runtime/ext/std/ext_std_network-posix.cpp
hphp/runtime/ext/std/ext_std_network-win.cpp
hphp/runtime/ext/std/ext_std_network.cpp
hphp/runtime/ext/std/ext_std_network.h
hphp/runtime/ext/std/ext_std_output.cpp
hphp/runtime/ext/std/ext_std_output.h
hphp/runtime/ext/std/ext_std_process.cpp
hphp/runtime/ext/std/ext_std_process.h
hphp/runtime/ext/stream/ext_stream.cpp
hphp/runtime/ext/stream/ext_stream.h
hphp/runtime/ext/string/ext_string.cpp
hphp/runtime/ext/string/ext_string.h
hphp/runtime/ext/thrift/binary.cpp
hphp/runtime/ext/thrift/compact.cpp
hphp/runtime/ext/thrift/ext_thrift.h
hphp/runtime/ext/url/ext_url.cpp
hphp/runtime/ext/url/ext_url.h
hphp/runtime/ext/watchman/ext_watchman.cpp
hphp/runtime/ext/xml/ext_xml.cpp
hphp/runtime/ext/xml/ext_xml.h
hphp/runtime/ext/zlib/ext_zlib.cpp
hphp/runtime/ext/zlib/ext_zlib.h
hphp/runtime/vm/native.cpp
hphp/runtime/vm/native.h
hphp/test/slow/ext_array/array_fill_large.php [new file with mode: 0644]
hphp/test/slow/ext_array/array_fill_large.php.expectf [new file with mode: 0644]
hphp/test/slow/ext_array/array_fill_negative.php [new file with mode: 0644]
hphp/test/slow/ext_array/array_fill_negative.php.expectf [new file with mode: 0644]
hphp/test/slow/ext_gd/t114251150.php [new file with mode: 0644]
hphp/test/slow/ext_gd/t114251150.php.expectf [new file with mode: 0644]