Skip to content

Commit

Permalink
Add sc_pid to sockcred so that SOCK_DGRAM and LOCAL_CREDS socket option
Browse files Browse the repository at this point in the history
can learn the process id of the AF_LOCAL sender.
Add compat glue for old structure.
  • Loading branch information
rsmarples committed Apr 6, 2016
1 parent 059fd57 commit 8e79650
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 15 deletions.
10 changes: 8 additions & 2 deletions share/man/man4/unix.4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $NetBSD: unix.4,v 1.24 2011/05/29 08:46:42 wiz Exp $
.\" $NetBSD: unix.4,v 1.25 2016/04/06 19:45:46 roy Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
Expand Down Expand Up @@ -29,7 +29,7 @@
.\"
.\" @(#)unix.4 8.1 (Berkeley) 6/9/93
.\"
.Dd May 29, 2011
.Dd March 31, 2016
.Dt UNIX 4
.Os
.Sh NAME
Expand Down Expand Up @@ -198,6 +198,7 @@ length sockcred structure, defined in
as follows:
.Bd -literal
struct sockcred {
pid_t sc_pid; /* process id */
uid_t sc_uid; /* real user id */
uid_t sc_euid; /* effective user id */
gid_t sc_gid; /* real group id */
Expand Down Expand Up @@ -289,3 +290,8 @@ macro, the following definition is recommended:
.%A Chris Torek
.Re
.Pq see Pa /usr/share/doc/psd/21.ipc
.Sh HISTORY
The
.Ar sc_pid
field was introduced in
.Nx 8.0 .
5 changes: 4 additions & 1 deletion sys/compat/common/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.55 2016/03/22 08:25:23 mrg Exp $
# $NetBSD: Makefile,v 1.56 2016/04/06 19:45:45 roy Exp $

LIB= compat
NOPIC= # defined
Expand Down Expand Up @@ -47,6 +47,9 @@ SRCS+= kern_50.c kern_time_50.c kern_select_50.c rndpseudo_50.c rtsock_50.c \
# Compatibility code for NetBSD 6.0
SRCS+= kern_sa_60.c tty_60.c kern_time_60.c

# Compatibility code for NetBSD 7.0
SRCS+= uipc_usrreq_70.c

# really, all machines where sizeof(int) != sizeof(long) (LP64)
.if (${MACHINE_ARCH} != "alpha" && ${MACHINE_ARCH} != "sparc64" \
&& ${MACHINE_ARCH} != "x86_64")
Expand Down
70 changes: 70 additions & 0 deletions sys/compat/common/uipc_usrreq_70.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* $NetBSD: uipc_usrreq_70.c,v 1.1 2016/04/06 19:45:45 roy Exp $ */

/*-
* Copyright (c) 2016 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Roy Marples.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq_70.c,v 1.1 2016/04/06 19:45:45 roy Exp $");

#include <sys/param.h>
#include <sys/lwp.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/unpcb.h>
#include <sys/mbuf.h>
#include <sys/kauth.h>

#include <compat/sys/socket.h>

#ifdef COMPAT_SOCKCRED70
struct mbuf *
compat_70_unp_addsockcred(struct lwp *l, struct mbuf *control)
{
struct sockcred70 *sc;
struct mbuf *m;
void *p;

m = sbcreatecontrol1(&p, SOCKCRED70SIZE(kauth_cred_ngroups(l->l_cred)),
SCM_OCREDS, SOL_SOCKET, M_WAITOK);
if (m == NULL)
return control;

sc = p;
sc->sc_uid = kauth_cred_getuid(l->l_cred);
sc->sc_euid = kauth_cred_geteuid(l->l_cred);
sc->sc_gid = kauth_cred_getgid(l->l_cred);
sc->sc_egid = kauth_cred_getegid(l->l_cred);
sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);

for (int i = 0; i < sc->sc_ngroups; i++)
sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);

return m_add(control, m);
}
#endif
24 changes: 23 additions & 1 deletion sys/compat/sys/socket.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: socket.h,v 1.12 2009/02/13 22:41:04 apb Exp $ */
/* $NetBSD: socket.h,v 1.13 2016/04/06 19:45:45 roy Exp $ */

/*
* Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
Expand Down Expand Up @@ -47,6 +47,10 @@
#define COMPAT_OSOCK
#endif

#ifdef COMPAT_70
#define COMPAT_SOCKCRED70
#endif

#else
#define COMPAT_OSOCK
#endif
Expand All @@ -71,19 +75,37 @@ struct omsghdr {
int msg_accrightslen;
};

/*
* 7.0 compat sockcred
*/
struct sockcred70 {
uid_t sc_uid; /* real user id */
uid_t sc_euid; /* effective user id */
gid_t sc_gid; /* real group id */
gid_t sc_egid; /* effective group id */
int sc_ngroups; /* number of supplemental groups */
gid_t sc_groups[1]; /* variable length */
};
#define SOCKCRED70SIZE(ngrps) \
(/*CONSTCOND*/sizeof(struct sockcred70) + (sizeof(gid_t) * \
((ngrps) ? ((ngrps) - 1) : 0)))

#ifdef _KERNEL

#define SO_OSNDTIMEO 0x1005
#define SO_ORCVTIMEO 0x1006
#define SO_OTIMESTAMP 0x0400
#define SCM_OTIMESTAMP 0x2
#define SCM_OCREDS 0x4

__BEGIN_DECLS
struct socket;
struct proc;
u_long compat_cvtcmd(u_long cmd);
int compat_ifioctl(struct socket *, u_long, u_long, void *, struct lwp *);
int compat43_set_accrights(struct msghdr *, void *, int);

struct mbuf * compat_70_unp_addsockcred(struct lwp *, struct mbuf *);
__END_DECLS
#else
int __socket30(int, int, int);
Expand Down
39 changes: 36 additions & 3 deletions sys/kern/uipc_usrreq.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: uipc_usrreq.c,v 1.179 2015/05/02 17:18:03 rtr Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.180 2016/04/06 19:45:45 roy Exp $ */

/*-
* Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -96,7 +96,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.179 2015/05/02 17:18:03 rtr Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.180 2016/04/06 19:45:45 roy Exp $");

#include <sys/param.h>
#include <sys/systm.h>
Expand All @@ -120,6 +120,10 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.179 2015/05/02 17:18:03 rtr Exp $"
#include <sys/kernel.h>
#include <sys/kthread.h>

#ifdef COMPAT_70
#include <compat/sys/socket.h>
#endif

/*
* Unix communications domain.
*
Expand Down Expand Up @@ -319,6 +323,10 @@ unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp)
sun = &sun_noname;
if (unp->unp_conn->unp_flags & UNP_WANTCRED)
control = unp_addsockcred(curlwp, control);
#ifdef COMPAT_SOCKCRED70
if (unp->unp_conn->unp_flags & UNP_OWANTCRED)
control = compat_70_unp_addsockcred(curlwp, control);
#endif
if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
control) == 0) {
so2->so_rcv.sb_overflowed++;
Expand Down Expand Up @@ -491,6 +499,16 @@ unp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
control = unp_addsockcred(l, control);
}
#ifdef COMPAT_SOCKCRED70
if (unp->unp_conn->unp_flags & UNP_OWANTCRED) {
/*
* Credentials are passed only once on
* SOCK_STREAM and SOCK_SEQPACKET.
*/
unp->unp_conn->unp_flags &= ~UNP_OWANTCRED;
control = compat_70_unp_addsockcred(l, control);
}
#endif
/*
* Send to paired receive port, and then reduce
* send buffer hiwater marks to maintain backpressure.
Expand Down Expand Up @@ -566,6 +584,9 @@ uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
switch (sopt->sopt_name) {
case LOCAL_CREDS:
case LOCAL_CONNWAIT:
#ifdef COMPAT_SOCKCRED70
case LOCAL_OCREDS:
#endif
error = sockopt_getint(sopt, &optval);
if (error)
break;
Expand All @@ -582,6 +603,11 @@ uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
case LOCAL_CONNWAIT:
OPTSET(UNP_CONNWAIT);
break;
#ifdef COMPAT_SOCKCRED70
case LOCAL_OCREDS:
OPTSET(UNP_OWANTCRED);
break;
#endif
}
break;
#undef OPTSET
Expand Down Expand Up @@ -609,6 +635,12 @@ uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
optval = OPTBIT(UNP_WANTCRED);
error = sockopt_setint(sopt, optval);
break;
#ifdef COMPAT_SOCKCRED70
case LOCAL_OCREDS:
optval = OPTBIT(UNP_OWANTCRED);
error = sockopt_setint(sopt, optval);
break;
#endif
#undef OPTBIT

default:
Expand Down Expand Up @@ -1572,8 +1604,9 @@ unp_addsockcred(struct lwp *l, struct mbuf *control)
SCM_CREDS, SOL_SOCKET, M_WAITOK);
if (m == NULL)
return control;

sc = p;
sc->sc_pid = l->l_proc->p_pid;
sc->sc_uid = kauth_cred_getuid(l->l_cred);
sc->sc_euid = kauth_cred_geteuid(l->l_cred);
sc->sc_gid = kauth_cred_getgid(l->l_cred);
Expand Down
3 changes: 2 additions & 1 deletion sys/modules/compat/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.12 2015/05/10 07:41:16 pgoyette Exp $
# $NetBSD: Makefile,v 1.13 2016/04/06 19:45:45 roy Exp $

.include "../Makefile.inc"

Expand Down Expand Up @@ -34,6 +34,7 @@ SRCS+= vfs_syscalls_20.c vfs_syscalls_30.c vfs_syscalls_40.c
SRCS+= vfs_syscalls_43.c vm_12.c vm_43.c compat_mod.c
SRCS+= kern_time_50.c kern_50.c vfs_syscalls_50.c
SRCS+= tty_60.c kern_time_60.c
SRCS+= uipc_usrreq_70.c

.PATH: ${S}/arch/${MACHINE}/${MACHINE}
.PATH: ${S}/arch/${MACHINE_ARCH}/${MACHINE_ARCH}
Expand Down
13 changes: 10 additions & 3 deletions sys/sys/socket.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: socket.h,v 1.118 2015/10/13 21:28:34 rjs Exp $ */
/* $NetBSD: socket.h,v 1.119 2016/04/06 19:45:46 roy Exp $ */

/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
Expand Down Expand Up @@ -335,6 +335,11 @@ struct sockaddr_storage {

#if defined(_NETBSD_SOURCE)

#ifndef pid_t
typedef __pid_t pid_t; /* process id */
#define pid_t __pid_t
#endif

#ifndef gid_t
typedef __gid_t gid_t; /* group id */
#define gid_t __gid_t
Expand All @@ -349,6 +354,7 @@ typedef __uid_t uid_t; /* user id */
* Socket credentials.
*/
struct sockcred {
pid_t sc_pid; /* process id */
uid_t sc_uid; /* real user id */
uid_t sc_euid; /* effective user id */
gid_t sc_gid; /* real group id */
Expand Down Expand Up @@ -595,9 +601,10 @@ struct cmsghdr {
/* "Socket"-level control message types: */
#define SCM_RIGHTS 0x01 /* access rights (array of int) */
#if defined(_NETBSD_SOURCE)
/* 0x02 timestamp (struct timeval50) */
#define SCM_CREDS 0x04 /* credentials (struct sockcred) */
/* 0x02 timestamp (struct timeval50) */
/* 0x04 credentials (struct sockcred70) */
#define SCM_TIMESTAMP 0x08 /* timestamp (struct timeval) */
#define SCM_CREDS 0x10 /* credentials (struct sockcred) */
#endif

/*
Expand Down
5 changes: 3 additions & 2 deletions sys/sys/un.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: un.h,v 1.56 2015/05/02 17:18:04 rtr Exp $ */
/* $NetBSD: un.h,v 1.57 2016/04/06 19:45:46 roy Exp $ */

/*
* Copyright (c) 1982, 1986, 1993
Expand Down Expand Up @@ -56,9 +56,10 @@ struct sockaddr_un {
* Socket options for UNIX IPC domain.
*/
#if defined(_NETBSD_SOURCE)
#define LOCAL_CREDS 0x0001 /* pass credentials to receiver */
#define LOCAL_OCREDS 0x0001 /* pass credentials to receiver */
#define LOCAL_CONNWAIT 0x0002 /* connects block until accepted */
#define LOCAL_PEEREID 0x0003 /* get peer identification */
#define LOCAL_CREDS 0x0004 /* pass credentials to receiver */
#endif

/*
Expand Down
5 changes: 3 additions & 2 deletions sys/sys/unpcb.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: unpcb.h,v 1.17 2008/04/24 11:38:39 ad Exp $ */
/* $NetBSD: unpcb.h,v 1.18 2016/04/06 19:45:46 roy Exp $ */

/*
* Copyright (c) 1982, 1986, 1989, 1993
Expand Down Expand Up @@ -97,11 +97,12 @@ struct unpcb {
* in with data for the listening process. This is set up in unp_bind() when
* it fills in unp_connid for later consumption by unp_connect().
*/
#define UNP_WANTCRED 0x0001 /* credentials wanted */
#define UNP_OWANTCRED 0x0001 /* credentials wanted */
#define UNP_CONNWAIT 0x0002 /* connect blocks until accepted */
#define UNP_EIDSVALID 0x0004 /* unp_connid contains valid data */
#define UNP_EIDSBIND 0x0008 /* unp_connid was set by bind() */
#define UNP_BUSY 0x0010 /* busy connecting or binding */
#define UNP_WANTCRED 0x0020 /* credentials wanted */

#define sotounpcb(so) ((struct unpcb *)((so)->so_pcb))

Expand Down

0 comments on commit 8e79650

Please sign in to comment.