News Categories

The trouble with struct sockaddr’s fake flexible array

The trouble with struct sockaddr’s fake flexible array

Views: 2850

By Jonathan CorbetNovember 7, 2024

Flexible arrays — arrays that are declared as the final member of a structure and which have a size determined at run time — have long drawn the attention of developers seeking to harden the kernel against buffer-overflow vulnerabilities. These arrays have reliably been a source of bugs, so anything that can be done to ensure that operations on them stay within bounds is a welcome improvement. While many improvements, including the recent counted-by work , have been made, one of the most difficult cases remains. Now, however, developers who are interested in using recent compiler bounds-checking features are trying to get a handle on

struct sockaddr

.

The many faces of struct sockaddr

The sockaddr structure dates back to the beginning of the BSD socket API; it is used to hold an address corresponding to one side of a network connection. The 4.2 BSD networking implementation notes from 1983 give its format as:

struct sockaddr { short sa_family; char sa_data[14]; };

The sa_family field describes which address family is in use — AF_INET for an IPv4 address, for example. sa_data holds the actual address, the format of which will vary depending on the family. The implementation notes say that: “the size of the data field, 14 bytes, was selected based on a study of current address formats”. In other words, 14 bytes — much longer than the four needed for an IPv4 address — should really be enough for anybody.

$ sudo subscribe today

Subscribe today and elevate your LWN privileges. You’ll have access to all of LWN’s high-quality articles as soon as they’re published, and help support LWN in the process. Act now and you can start with a free trial subscription.

Need it be said that 14 bytes turned out not to be enough? As new protocols came along, they brought address formats that were longer than would fit in sa_data. But the sockaddr structure was firmly set in stone as user-space API and could not be changed. It appears in essentially the same form in any modern Unix-like system; on Linux the type of sa_family is now sa_family_t, but otherwise the structure is the same.

The result was one of the (many) historic kludges of the Unix API. New protocol implementations typically brought


Discover more from 25finz, L.L.C

Subscribe to get the latest posts sent to your email.

Social Media Auto Publish Powered By : XYZScripts.com

15% Off Everything

Days
Hours
Minutes
Seconds

Starting 11/18-11/29