Skip to content

Addrinfo.getaddrinfo silently truncates mrb_int family/socktype/protocol/flags to int #6960

Description

@tsx543

Summary

In mrbgems/mruby-socket/src/socket.c, mrb_addrinfo_getaddrinfo() accepts Ruby integers for family, socktype, protocol, and flags, then casts them directly to C int before calling getaddrinfo():

hints.ai_flags = (int)flags;
if (mrb_integer_p(family)) {
  hints.ai_family = (int)mrb_integer(family);
}
if (mrb_integer_p(socktype)) {
  hints.ai_socktype = (int)mrb_integer(socktype);
}
if (mrb_integer_p(protocol)) {
  hints.ai_protocol = (int)mrb_integer(protocol);
}

On default 64-bit mruby builds, mrb_int is 64-bit. Out-of-range values are not rejected; they are silently truncated to 32-bit int. After truncation they may collide with valid low socket constants, so getaddrinfo() may proceed with unintended parameters instead of failing validation.

Impact

  • Silent numeric truncation / weak input validation.
  • Caller-supplied out-of-range integers can become different, still-"valid looking" hint values.
  • Not a demonstrated memory-corruption bug, but incorrect argument handling for a public socket API.

Affected

  • File: mrbgems/mruby-socket/src/socket.c
  • Function: mrb_addrinfo_getaddrinfo
  • Version tested: latest master @ c04daf59b41d

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions