Skip to content

socket: fix memory errors and exception handling in the socket library - #525

Open
nbd168 wants to merge 10 commits into
ucode-lang:masterfrom
nbd168:fixes-socket
Open

nbd168 wants to merge 10 commits into
ucode-lang:masterfrom
nbd168:fixes-socket

Conversation

@nbd168

@nbd168 nbd168 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This series fixes defects in lib/socket.c. Most of them need script
code that runs during a call: a tostring() or fileno() method that
changes the arguments of the call, or that raises an exception.

Memory errors:

  • sendmsg() read released memory if a tostring() method changed the
    data array during the conversion of a later item.
  • sendmsg() wrote past the control buffer if a fileno() method added
    entries to an SCM_RIGHTS list, and read a released list if the
    method removed it from the message.
  • recvmsg() and getopt() used a buffer that they could not allocate.
    sk.recvmsg(3000000000) ended the program with SIGSEGV.

Exceptions:

  • send() and sendmsg() sent the partial text if a tostring() method
    raised an exception or called exit(). They now send nothing.
  • sendmsg() left out a descriptor and sent the message if a fileno()
    method raised an exception. It now fails with the exception.
  • poll() lost the exception of a fileno() method when it called the
    next one. It now ends at the exception.

Other fixes:

  • The objects built from C structures (addrinfo() entries, socket
    options with several fields, control message data) were unknown to
    the collector. It never collected a cycle through them.
  • getopt() returned the names of TCP_CONGESTION, SO_BINDTODEVICE and
    SO_PEERSEC with the bytes after the terminating NUL byte. A new
    option type ends the value at the first NUL byte. A separate commit
    first renames the type SV_STRING to SV_BINARY, without functional
    change, so that SV_STRING names the new type.

uc_socket_inst_sendmsg() pointed the iov entry of a string item into the
data array and then converted the later items. The tostring method of
such an item is script code. It can add items, which moves the storage
of the array, or replace an item, which releases its string. sendmsg()
then read released memory:

  Syscall param sendmsg(msg.msg_iov[0]) points to unaddressable byte(s)
    by uc_socket_inst_sendmsg (socket.c)
  Address is 1 bytes inside a block of size 48 free'd
    by ucv_array_push (types.c)
    by uc_push (lib.c)
    by ucv_call_tostring (types.c)
    by uc_socket_inst_sendmsg (socket.c)

Take an own reference to each item as a string before the iov array is
filled. The message holds each item as it was when the function read it.

Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Signed-off-by: Felix Fietkau <[email protected]>
encode_cmsg() borrows the data of a control message from the message
object, and uc_socket_inst_sendmsg() sizes the control buffer before the
conversion. The fileno method of an entry of an SCM_RIGHTS list is
script code. It can remove the list from the object, which releases it
while the loop still reads it. It can also add entries, so that the
descriptors no longer fit the buffer:

  AddressSanitizer: heap-buffer-overflow
  WRITE of size 260
    #0 memcpy
    ucode-lang#1 encode_cmsg lib/socket.c
    ucode-lang#2 uc_socket_inst_sendmsg lib/socket.c

Hold the data during the conversion, and fail the call if the converted
data exceeds the space that the buffer has for it.

Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Signed-off-by: Felix Fietkau <[email protected]>
encode_cmsg() leaves out an entry of an SCM_RIGHTS list that
uv_to_fileno() cannot convert, also if the fileno() method of the entry
raised an exception. The call of the fileno() method of a later entry
clears the exception, and sendmsg() sends the message without the
descriptor. An exception of the last entry reaches the script only
after the message went out.

Stop the conversion at the exception and fail the call with it, so that
sendmsg() sends nothing.

Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Signed-off-by: Felix Fietkau <[email protected]>
struct_to_uv() and the callbacks that convert an address member create
their objects without a VM, so the collector does not know them. These
objects are the entries and addresses of addrinfo(), the socket options
with several fields and the data of a control message. A cycle through
them alone was never collected:

  let addr = socket.addrinfo("127.0.0.1", 80)[0].addr;
  addr.self = addr;

Create the objects with the VM of the calling function.

Fixes: e8d78a2 ("lib: introduce socket library")
Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Signed-off-by: Felix Fietkau <[email protected]>
uc_socket_inst_recvmsg() does not check the result of strbuf_alloc(),
which is NULL for a size that strbuf_grow() rejects. strbuf_data() then
reads through the null pointer and the program ends with SIGSEGV:

  sk.recvmsg(3000000000);

Return null with the error of the allocation, as recv() does, for a
single size and for each size of a list. uc_socket_inst_getopt() gets
the same check, and releases its buffer if the buffer cannot grow.

Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Fixes: e8d78a2 ("lib: introduce socket library")
Signed-off-by: Felix Fietkau <[email protected]>
The send() method of a socket converts data that is not a string to
text. If a tostring method raises an exception or calls exit() during
the conversion, send() sends the partial text.

Send nothing if the conversion raised an exception.

Fixes: 953f36c ("socket: make socket.send() accept non-string data")
Signed-off-by: Felix Fietkau <[email protected]>
sendmsg() converts data that is not a string, and each item of a data
array that is not a string, to text. If a tostring method raises an
exception or calls exit() during a conversion, sendmsg() sends the
partial text.

Send nothing if a conversion raised an exception.

Fixes: 7611487 ("socket: implement recvmsg(), sendmsg() and cmsg support")
Signed-off-by: Felix Fietkau <[email protected]>
uc_socket_poll() leaves out an argument that uv_to_pollfd() cannot
convert, also if the fileno() method of the argument raised an
exception. The call of the fileno() method of a later argument clears
the exception, and poll() waits without the argument and returns a
result.

Return at the exception.

Fixes: e8d78a2 ("lib: introduce socket library")
Signed-off-by: Felix Fietkau <[email protected]>
The options of the type SV_STRING pass the bytes of a string value to
setsockopt() and return the bytes that getsockopt() reports, in the
reported length. Most of them hold binary data, such as IP options,
extension headers and filters.

Name the type after that. No functional change.

Signed-off-by: Felix Fietkau <[email protected]>
uc_socket_inst_getopt() returns the bytes of a binary option in the
length that getsockopt() reports. For an option that holds a name, this
length includes the NUL bytes after the name: TCP_CONGESTION gives
"cubic" followed by 11 NUL bytes, and SO_BINDTODEVICE gives "lo" with
one.

Add the option type SV_STRING, whose value ends at its first NUL byte,
and use it for TCP_CONGESTION, SO_BINDTODEVICE and SO_PEERSEC.

Fixes: e8d78a2 ("lib: introduce socket library")
Signed-off-by: Felix Fietkau <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant