From 4adb481be74ed510b2e47523fd96e51ed524b9b3 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 19 May 2025 09:04:07 -0700 Subject: [PATCH] Fix crash in getifaddrs We don't support AF_NETLINK sockets so just fail early here. Replaces #24187 --- src/lib/libsockfs.js | 8 ++++++++ test/other/test_getifaddrs.c | 12 ++++++++++++ test/other/test_getifaddrs.out | 1 + test/test_other.py | 3 +++ 4 files changed, 24 insertions(+) create mode 100644 test/other/test_getifaddrs.c create mode 100644 test/other/test_getifaddrs.out diff --git a/src/lib/libsockfs.js b/src/lib/libsockfs.js index 786606c3c39bc..7ada365896fdc 100644 --- a/src/lib/libsockfs.js +++ b/src/lib/libsockfs.js @@ -44,7 +44,15 @@ addToLibrary({ return FS.createNode(null, '/', {{{ cDefs.S_IFDIR | 0o777 }}}, 0); }, createSocket(family, type, protocol) { + // Emscripten only supports AF_INET + if (family != {{{ cDefs.AF_INET }}}) { + throw new FS.ErrnoError({{{ cDefs.EAFNOSUPPORT }}}); + } type &= ~{{{ cDefs.SOCK_CLOEXEC | cDefs.SOCK_NONBLOCK }}}; // Some applications may pass it; it makes no sense for a single process. + // Emscripten only supports SOCK_STREAM and SOCK_DGRAM + if (type != {{{ cDefs.SOCK_STREAM }}} && type != {{{ cDefs.SOCK_DGRAM }}}) { + throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); + } var streaming = type == {{{ cDefs.SOCK_STREAM }}}; if (streaming && protocol && protocol != {{{ cDefs.IPPROTO_TCP }}}) { throw new FS.ErrnoError({{{ cDefs.EPROTONOSUPPORT }}}); // if SOCK_STREAM, must be tcp or 0. diff --git a/test/other/test_getifaddrs.c b/test/other/test_getifaddrs.c new file mode 100644 index 0000000000000..a0d1855a4fe2f --- /dev/null +++ b/test/other/test_getifaddrs.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include + +int main() { + struct ifaddrs* ifaddrs = NULL; + int rtn = getifaddrs(&ifaddrs); + printf("getifaddrs => %d (%s)\n", rtn, strerror(errno)); + return 0; +} diff --git a/test/other/test_getifaddrs.out b/test/other/test_getifaddrs.out new file mode 100644 index 0000000000000..3ce6ae890f4b0 --- /dev/null +++ b/test/other/test_getifaddrs.out @@ -0,0 +1 @@ +getifaddrs => -1 (Address family not supported by protocol) diff --git a/test/test_other.py b/test/test_other.py index 29194ca81bf30..28b877f93379a 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -16092,3 +16092,6 @@ def test_em_js_bool_macro_expansion(self): } ''') self.do_runf('main.c') + + def test_getifaddrs(self): + self.do_other_test('test_getifaddrs.c')