haskell / haskell/network

Incorrect handling of `address` argument in recvfrom()

Open
#539 10 comments 0 reactions 1 assignee Claimed by @kazu-yamamoto View on GitHub
Dominant language
Haskell
Stars
366
Forks
210
Avg merge
22h 38m
Merged PRs (30d)
5

Description

### Context:
`recvfrom()` stores the source address of a received package in the `sockaddr` structure pointed to by the `address` pointer and the length of the address is stored in the memory pointed to by the `address_len` argument.

### Problem description:
However, not all protocol provide source addresses, e.g. AF_UNIX does not.
In this case, the contents of the `address` parameter is "unspecified" [1] and should not be inspected. I have not found it in the specification, but in practice I have observed that at least on Linux `address_len` is set to 0

`recvBufFrom` does not check `address_len` at all and instead tries to parse the contents of `address` directly [2]. This leads to an error because it interprets the zeroed memory as `AF_UNSPEC` which is not supported. The error is ignored and `getPeerName` is called, which also fails.

The solution is to `peek ptr_len` and check if it is `0`, and if so to return an "unset" address. I'm not sure how best to represent such an address, perhaps `SockAddrUnix []` would do the trick?

Compare also how Rust handles the situation [3]

I have included a minimal code example that reproduced the problem [4]

* [1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html
* [2] https://github.com/haskell/network/blob/26e9d3cd57587e3f102d7bf722c18edc3e7172ae/Network/Socket/Buffer.hsc#L114-L126
* [3] https://doc.rust-lang.org/src/std/os/unix/net/addr.rs.html#109-113
* [4] https://gist.github.com/Philonous/7fd9d6620e631325f6bd2fffaba0cc2e

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.