title: Mismatch between manpage and kernel behavior about getsockname
body: <p>I've experienced a smashing stack (= buffer overflow) problem recently when trying to run <code>iperf3</code>. I pinpointed the reason to the <code>getsockname()</code> call (<a href="https://github.com/esnet/iperf/blob/master/src/net.c#L463" rel="nofollow">https://github.com/esnet/iperf/blob/master/src/net.c#L463</a>) that makes the kernel copy more data (<code>sizeof(sin_addr)</code>) at the designed address (<code>&amp;sa</code>) than the size of the variable on the stack at that address.
  <code>getsockname()</code> redirects the call to <code>getname()</code> (<code>AF_INET</code> family) :
  <a href="https://github.com/torvalds/linux/blob/master/net/ipv4/af_inet.c#L698" rel="nofollow">https://github.com/torvalds/linux/blob/master/net/ipv4/af_inet.c#L698</a></p>
  
  <p>If I believe the manpage (ubuntu) it says:</p>
  
  <pre class="lang-c prettyprint-override"><code>int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);  
  </code></pre>
  
  <blockquote>
    <p>The <code>addrlen</code> argument should be initialized to indicate the amount of space (in bytes) pointed to by <code>addr</code>.  On return it contains the actual size of the socket address.</p>
    
    <p>The returned address is truncated if the buffer provided is too small; in this case, <code>addrlen</code> will return a value greater than was supplied to the call.</p>
  </blockquote>
  
  <p>But in the previous code excerpt, <code>getname()</code> does not care about the <code>addrlen</code> input value and uses the parameter as an output value only.</p>
  
  <p>I had found a link (can't find it anymore) saying that BSD respects the previous manpage excerpt contrary to linux.</p>
  
  <p>Am I missing something? I find it awkward that the documentation would be that much off, I've checked other linux <code>XXX_getname</code> calls and all I saw didn't care about the input length.</p>