NetBSD Problem Report #21577

Received: (qmail 18798 invoked by uid 605); 14 May 2003 17:22:53 -0000
Message-Id: <200305141719.h4EHJJ87002099@server.duh.org>
Date: Wed, 14 May 2003 13:19:19 -0400 (EDT)
From: tv@pobox.com
Sender: gnats-bugs-owner@netbsd.org
Reply-To: tv@pobox.com
To: gnats-bugs@gnats.netbsd.org
Subject: linux_sys_sendmsg() no worko
X-Send-Pr-Version: 3.95

>Number:         21577
>Category:       kern
>Synopsis:       linux_sys_sendmsg() no worko
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    jdolecek
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed May 14 17:23:00 +0000 2003
>Closed-Date:    Wed Jul 30 14:15:50 +0000 2003
>Last-Modified:  Wed Jul 30 14:15:50 +0000 2003
>Originator:     Todd Vierling
>Release:        NetBSD 1.6.1_STABLE
>Organization:
	DUH.ORG:  Pointing out the obvious since 1994.
>Environment:
System: NetBSD server.duh.org 1.6.1_STABLE NetBSD 1.6.1_STABLE (HOME) #1: Wed May 14 12:33:57 EDT 2003 tv@server.duh.org:/export/SRC/duh/netbsd-kernels/HOME i386
Architecture: i386
Machine: i386
>Description:

[Technically this falls under "kern", not "port-i386", although there are
code comments that suggest that linux_sys_sendmsg() needs alignment fixups
in order to work at all on any other platform.]

The linux_sys_sendmsg() call does not work if given a msg_name of NULL, as
is typical when using SCM_RIGHTS to pass a fd from one side of a
socketpair to the other.  This results in an EFAULT when the real
sys_sendmsg() tries to get the msghdr.  A fix is provided (diff below).

Even after fixing this problem, passing fds results in EINVAL somewhere
after entering sys_sendmsg() -- regardless of whether msg_name is filled
in or not...!  I haven't been able to find what is causing the EINVAL yet,
so I pose this to others who may know the socket goop better than I.  (If
no one has immediate insight, I'll probably have to fire up a
debug-compiled kernel under VMware and start single-stepping.  8-)

>How-To-Repeat:

Sample program to illustrate the point.

Hint:  Yes, it looks similar to Wine's scheduler/client.c, as that is
precisely what I'm trying to fix.  Fixing this bug has a high probability
of making the bundled Wine in CrossOver Office work correctly under
emulation.

========== fdtest.c ==========
#include <err.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

struct msghdr mh = {0};

struct cmsg_fd {
	int len;
	int level;
	int type;
	int fd;
} cm = { sizeof(cm), SOL_SOCKET, SCM_RIGHTS, -1 };

int main(void) {
	int fds[2];
	int buf = 1;
	struct iovec iov = {&buf, sizeof buf};

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
		err(1, "socketpair");

	warnx("sending fd %d", STDOUT_FILENO);

	cm.fd = STDOUT_FILENO;
	mh.msg_iov = &iov;
	mh.msg_iovlen = 1;
	mh.msg_control = &cm;
	mh.msg_controllen = sizeof(cm);

	if (sendmsg(fds[0], &mh, 0) < 0)
		err(1, "sendmsg");

	if (recvmsg(fds[1], &mh, 0) < 0)
		err(1, "recvmsg");

	warnx("received fd %d", cm.fd);

	write(5, "test output\n", 12);

	return 0;
}
==========

Expected output:

  fdtest: sending fd 1
  fdtest: received fd 5
  test output

Invalid Linux emulation output (before diff below):

  fdtest: sending fd 1
  fdtest: sendmsg: Bad address

Invalid Linux emulation output (after diff below):

  fdtest: sending fd 1
  fdtest: sendmsg: Invalid argument

>Fix:

The following diff fixes the msgbuf pointer problem by properly
initializing "nmsg" in the case that it is not reallocated in the stackgap
and rewritten.  This eliminates the erroneous EFAULT in sys_sendmsg().

I recommend committing this diff and pulling it up to 1.6, regardless of
the resolution of the pending EINVAL problem above.

===================================================================
--- compat/linux/common/linux_socket.c	2002/05/12 18:30:32	1.39
+++ compat/linux/common/linux_socket.c	2003/05/14 17:06:12
@@ -300,7 +300,7 @@
 	struct msghdr	msg;
 	int		error;
 	struct sys_sendmsg_args bsa;
-	struct msghdr *nmsg = NULL;
+	struct msghdr *nmsg = SCARG(uap, msg);

 	error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
 	if (error)
===================================================================
>Release-Note:
>Audit-Trail:

From: Todd Vierling <tv@pobox.com>
To: gnats-bugs@gnats.netbsd.org
Cc: netbsd-bugs@netbsd.org
Subject: Re: kern/21577: linux_sys_sendmsg() no worko
Date: Wed, 14 May 2003 13:43:21 -0400 (EDT)

 On Wed, 14 May 2003 tv@pobox.com wrote:

 : ========== fdtest.c ==========

 A statically linked, bzip2ed Linux binary is available currently at:

 ftp://ftp.duh.org/pub/NetBSD/linux-fdtest.bz2

 -- 
 -- Todd Vierling <tv@pobox.com>
Responsible-Changed-From-To: kern-bug-people->jdolecek 
Responsible-Changed-By: jdolecek 
Responsible-Changed-When: Sun Jun 22 08:32:33 UTC 2003 
Responsible-Changed-Why:  
I'll handle this. 
State-Changed-From-To: open->analyzed 
State-Changed-By: jdolecek 
State-Changed-When: Wed Jul 23 09:25:00 UTC 2003 
State-Changed-Why:  
The second problem is due to incorrect alignment of cmsgdata. Apparently 
Linux doesn't use any alignment in the userland structure, and linux 
kernel converts the strcture to kernel representation on the syscall. 
Disgusting :( Once again, I'm very pleased I don't do Linux. 

Anyway, since I now know what's the problem, I'll fix it 'soon', 
hopefully this weekend. 

From: Todd Vierling <tv@pobox.com>
To: gnats-bugs@gnats.netbsd.org, netbsd-bugs@netbsd.org
Cc: jdolecek@netbsd.org
Subject: Re: kern/21577
Date: Wed, 23 Jul 2003 07:54:03 -0400 (EDT)

 On Wed, 23 Jul 2003 jdolecek@netbsd.org wrote:

 : The second problem is due to incorrect alignment of cmsgdata.

 I've found that on i386, this isn't the reason for the EINVAL.  (Actually, I
 think i386 NetBSD and Linux are using the same alignment for cmsgdata, but I
 may be wrong.)

 In any case, the following hack is currently in my tree, which addresses the
 EINVAL issue.  I haven't yet tested it with *multiple* cmsg structures,
 which would test out your alignment assertion above, but I know that it
 makes passing a single fd work properly under Linux emulation.

 I've considered going backwards-corkscrew on this and putting #ifdef
 COMPAT_LINUX in uipc_usrreq.c, but that's still ugly.  The "real" fix would
 be to convert Linux SOL_SOCKET (1) to NetBSD SOL_SOCKET (0xffff) on
 sendmsg(), and convert it back on recvmsg() -- albeit one Hell of a lot of
 stackgap munging.  The part on which I'm uneasy is linux_sys_recvmsg(),
 which might need to pull out parts of the NetBSD sys_recvmsg() and work
 directly with the kernel structures rather than doing a stackgap backflip.

 Index: kern/uipc_usrreq.c
 ===================================================================
 RCS file: /cvsroot/src/sys/kern/uipc_usrreq.c,v
 retrieving revision 1.53
 diff -u -r1.53 uipc_usrreq.c
 --- kern/uipc_usrreq.c	2001/11/12 15:25:34	1.53
 +++ kern/uipc_usrreq.c	2003/07/23 11:52:24
 @@ -926,7 +926,7 @@
  	u_int neededspace;

  	/* Sanity check the control message header */
 -	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
 +	if (cm->cmsg_type != SCM_RIGHTS || (cm->cmsg_level != SOL_SOCKET && cm->cmsg_level != 1) ||
  	    cm->cmsg_len != control->m_len)
  		return (EINVAL);

 -- 
 -- Todd Vierling <tv@pobox.com>
State-Changed-From-To: analyzed->feedback 
State-Changed-By: jdolecek 
State-Changed-When: Sun Jul 27 19:30:23 UTC 2003 
State-Changed-Why:  
You were right, it was only matter of CMSG level value on i386, 
the alignment is same. 
I checked in a fix for -current, which appears to fix the problem - your 
test program runs correctly, and even gets proper (Linux) value 
for cmsg_level after recvmsg(). Any change for you to try it out, 
or should I just close this PR? 

From: Todd Vierling <tv@duh.org>
To: gnats-bugs@gnats.netbsd.org
Cc: jdolecek@netbsd.org
Subject: Re: kern/21577
Date: Wed, 30 Jul 2003 10:08:37 -0400 (EDT)

 On Sun, 27 Jul 2003 jdolecek@netbsd.org wrote:

 : I checked in a fix for -current, which appears to fix the problem - your
 : test program runs correctly, and even gets proper (Linux) value
 : for cmsg_level after recvmsg(). Any change for you to try it out,
 : or should I just close this PR?

 I'm confident in your assessment, so you may close the PR.

 I'll be doing my own more extensive testing with a backported-to-1.6 version
 of the fix, since my -current testing ability is low at the moment.

 -- 
 -- Todd Vierling <tv@pobox.com>
State-Changed-From-To: feedback->closed 
State-Changed-By: jdolecek 
State-Changed-When: Wed Jul 30 14:15:06 UTC 2003 
State-Changed-Why:  
The problem should be fixed now. Thanks for report and analysis of the root 
cause of the problem. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.