delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2013/02/03/21:04:04

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-3.1 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,KHOP_RCVD_TRUST,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
X-Received: by 10.112.49.106 with SMTP id t10mr2289475lbn.6.1359943425368; Sun, 03 Feb 2013 18:03:45 -0800 (PST)
Date: Mon, 4 Feb 2013 11:03:45 +0900
Message-ID: <CANjopZHFhNN=o4cVpiBokBmDHjzgFx8tV=ajzLeUHzu4Jpr8EQ@mail.gmail.com>
Subject: bind(sock, addr, addrlen) doesn't respect addrelen
From: Tanaka Akira <akr AT fsij DOT org>
To: cygwin AT cygwin DOT com
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Hi.

I found bind(sock, addr, addrlen) function doesn't respect addrlen.

If addr is AF_UNIX socket address and sun_path field is not
NUL-terminated until the length specified as addrlen,
bind() refer bytes after addrlen.
This can be observed by created socket file name is longer
than expected.

The test program attached below specifies addrlen as
offsetof(struct sockaddr_un, sun_path) + 2.
So I expect bind() uses sun_path[0] and sun_path[1] and
doen't use sun_path[2] and later.
But actually created socket is "abcd".
This means bind() uses sun_path[0] to sun_path[3]
(and the terminating NUL in sun_path[4]).

I know POSIX defines sun_path as pathname and
pathname is NUL-terminated by definition.
So strictly speaking, this test is not POSIX conforming.

But I feel the given length should be respected and
other platforms such as GNU/Linux, FreeBSD, NetBSD,
OpenBSD and SunOS respects addrlen.

I found this problem when I made a more generic test program:
https://github.com/akr/socket-test
result: http://htmlpreview.github.com/?https://github.com/akr/socket-test/blob/master/results.html

% uname -mrsv
CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686
% ls
tst.c
% gcc -Wall tst.c -o tst
% ls
tst.c  tst.exe
% ./tst
% ls
abcd  tst.c  tst.exe
% file abcd
abcd: socket
% cat tst.c
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>

int main(int argc, char *argv[])
{
  int s, ret;
  struct sockaddr_un addr;
  socklen_t addrlen;

  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1) { perror("socket"); exit(EXIT_FAILURE); }

  addrlen = offsetof(struct sockaddr_un, sun_path) + 2;
  addr.sun_family = AF_UNIX;
  addr.sun_path[0] = 'a';
  addr.sun_path[1] = 'b';
  addr.sun_path[2] = 'c';
  addr.sun_path[3] = 'd';
  addr.sun_path[4] = '\0';

  ret = bind(s, (struct sockaddr *)&addr, addrlen);
  if (s == -1) { perror("bind"); exit(EXIT_FAILURE); }

  return EXIT_SUCCESS;
}
-- 
Tanaka Akira

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019