From cb977d3ada713ffe9248c87f4bf8ac789c75cd0c Mon Sep 17 00:00:00 2001 From: mvs Date: Thu, 9 Dec 2021 17:25:54 +0000 Subject: [PATCH] Add sys/kern/unixsockets directory to contain all unix(4) sockets tests. Add 'undgram_selfconn' test to check unix(4) socket connecting to itself. Discussed and ok visa@ bluhm@ --- regress/sys/kern/Makefile | 4 +- regress/sys/kern/unixsockets/Makefile | 23 ++++++ .../sys/kern/unixsockets/undgram_selfconn.c | 81 +++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 regress/sys/kern/unixsockets/Makefile create mode 100644 regress/sys/kern/unixsockets/undgram_selfconn.c diff --git a/regress/sys/kern/Makefile b/regress/sys/kern/Makefile index 00daa065c97..23aeacf8eff 100644 --- a/regress/sys/kern/Makefile +++ b/regress/sys/kern/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.95 2021/11/29 13:05:04 mvs Exp $ +# $OpenBSD: Makefile,v 1.96 2021/12/09 17:25:54 mvs Exp $ SUBDIR+= __syscall SUBDIR+= accept access @@ -24,7 +24,7 @@ SUBDIR+= setuid SUBDIR+= signal sosplice stackjmp stackpivot syscall syscall_segment SUBDIR+= sysvmsg sysvsem sysvshm SUBDIR+= unalign unconacc undgram_conclose unfdpass unfdpassfail ungc -SUBDIR+= unsendrecvthr unixsock unveil unveil-unmount +SUBDIR+= unixsockets unsendrecvthr unixsock unveil unveil-unmount SUBDIR+= wait install: diff --git a/regress/sys/kern/unixsockets/Makefile b/regress/sys/kern/unixsockets/Makefile new file mode 100644 index 00000000000..f3f1a81c38f --- /dev/null +++ b/regress/sys/kern/unixsockets/Makefile @@ -0,0 +1,23 @@ +# $OpenBSD: Makefile,v 1.1 2021/12/09 17:25:54 mvs Exp $ + +# Copyright (c) 2021 Makkoveev Vitaliy +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +WARNINGS = yes + +PROGS = undgram_selfconn + +CLEANFILES += *.socket + +.include diff --git a/regress/sys/kern/unixsockets/undgram_selfconn.c b/regress/sys/kern/unixsockets/undgram_selfconn.c new file mode 100644 index 00000000000..332188057be --- /dev/null +++ b/regress/sys/kern/unixsockets/undgram_selfconn.c @@ -0,0 +1,81 @@ +/* $OpenBSD: undgram_selfconn.c,v 1.1 2021/12/09 17:25:54 mvs Exp $ */ + +/* + * Copyright (c) 2021 Vitaliy Makkoveev + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * unix(4) datagram socket could be connected to itself. There are two + * cases: temporary connection for sendto(2) syscall and normal connection + * provided by connect(2) syscall. + * Be sure socket doesn't deadlock itself and doesn't crash kernel while + * disconnecting and connecting again. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(void) +{ + static struct sockaddr_un sun; + int s, buf; + ssize_t ret; + + umask(0077); + + memset(&sun, 0, sizeof(sun)); + sun.sun_len = sizeof(sun); + sun.sun_family = AF_UNIX; + snprintf(sun.sun_path, sizeof(sun.sun_path) - 1, + "undgram_selfconn%d.socket", getpid()); + + unlink(sun.sun_path); + + if ((s = socket(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0)) < 0) + err(1, "socket"); + if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) + err(1, "bind"); + + if (sendto(s, &s, sizeof(s), 0, + (struct sockaddr *)&sun, sizeof(sun)) < 0) + err(1, "sendto"); + + /* + * Check received data to be sure the temporary connection + * was successful. + */ + if ((ret = recvfrom(s, &buf, sizeof(buf), 0, NULL, NULL)) < 0) + err(1, "recvfrom"); + if (ret != sizeof(s)) + errx(1, "recvfrom: wrong size"); + if (buf != s) + errx(1, "recvfrom: wrong data"); + + if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) + err(1, "connect"); + /* Disconnect and connect it again */ + if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) + err(1, "connect"); + close(s); + + return 0; +} -- 2.20.1