diff --git a/sys/kern/uipc_accf.c b/sys/kern/uipc_accf.c index c63b5a1179bc..9e30e7839103 100644 --- a/sys/kern/uipc_accf.c +++ b/sys/kern/uipc_accf.c @@ -1,307 +1,311 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2000 Paycounter, Inc. * Copyright (c) 2005 Robert N. M. Watson * Author: Alfred Perlstein , * All rights reserved. * * 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 AUTHOR 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 AUTHOR 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 #define ACCEPT_FILTER_MOD #include "opt_param.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include static struct mtx accept_filter_mtx; MTX_SYSINIT(accept_filter, &accept_filter_mtx, "accept_filter_mtx", MTX_DEF); #define ACCEPT_FILTER_LOCK() mtx_lock(&accept_filter_mtx) #define ACCEPT_FILTER_UNLOCK() mtx_unlock(&accept_filter_mtx) static SLIST_HEAD(, accept_filter) accept_filtlsthd = SLIST_HEAD_INITIALIZER(accept_filtlsthd); MALLOC_DEFINE(M_ACCF, "accf", "accept filter data"); static int unloadable = 0; SYSCTL_NODE(_net, OID_AUTO, accf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "Accept filters"); SYSCTL_INT(_net_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, "Allow unload of accept filters (not recommended)"); /* * Must be passed a malloc'd structure so we don't explode if the kld is * unloaded, we leak the struct on deallocation to deal with this, but if a * filter is loaded with the same name as a leaked one we re-use the entry. */ int accept_filt_add(struct accept_filter *filt) { struct accept_filter *p; ACCEPT_FILTER_LOCK(); SLIST_FOREACH(p, &accept_filtlsthd, accf_next) if (strcmp(p->accf_name, filt->accf_name) == 0) { if (p->accf_callback != NULL) { ACCEPT_FILTER_UNLOCK(); return (EEXIST); } else { p->accf_callback = filt->accf_callback; ACCEPT_FILTER_UNLOCK(); free(filt, M_ACCF); return (0); } } if (p == NULL) SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next); ACCEPT_FILTER_UNLOCK(); return (0); } int accept_filt_del(char *name) { struct accept_filter *p; p = accept_filt_get(name); if (p == NULL) return (ENOENT); p->accf_callback = NULL; return (0); } struct accept_filter * accept_filt_get(char *name) { struct accept_filter *p; ACCEPT_FILTER_LOCK(); SLIST_FOREACH(p, &accept_filtlsthd, accf_next) if (strcmp(p->accf_name, name) == 0) break; ACCEPT_FILTER_UNLOCK(); return (p); } int accept_filt_generic_mod_event(module_t mod, int event, void *data) { struct accept_filter *p; struct accept_filter *accfp = (struct accept_filter *) data; int error; switch (event) { case MOD_LOAD: p = malloc(sizeof(*p), M_ACCF, M_WAITOK); bcopy(accfp, p, sizeof(*p)); error = accept_filt_add(p); break; case MOD_UNLOAD: /* * Do not support unloading yet. we don't keep track of * refcounts and unloading an accept filter callback and then * having it called is a bad thing. A simple fix would be to * track the refcount in the struct accept_filter. */ if (unloadable != 0) { error = accept_filt_del(accfp->accf_name); } else error = EOPNOTSUPP; break; case MOD_SHUTDOWN: error = 0; break; default: error = EOPNOTSUPP; break; } return (error); } int accept_filt_getopt(struct socket *so, struct sockopt *sopt) { struct accept_filter_arg *afap; int error; error = 0; afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK | M_ZERO); SOCK_LOCK(so); if (!SOLISTENING(so)) { error = EINVAL; goto out; } if (so->sol_accept_filter == NULL) { error = EINVAL; goto out; } strcpy(afap->af_name, so->sol_accept_filter->accf_name); if (so->sol_accept_filter_str != NULL) strcpy(afap->af_arg, so->sol_accept_filter_str); out: SOCK_UNLOCK(so); if (error == 0) error = sooptcopyout(sopt, afap, sizeof(*afap)); free(afap, M_TEMP); return (error); } int accept_filt_setopt(struct socket *so, struct sockopt *sopt) { struct accept_filter_arg *afap; struct accept_filter *afp; char *accept_filter_str = NULL; void *accept_filter_arg = NULL; int error; /* * Handle the simple delete case first. */ if (sopt == NULL || sopt->sopt_val == NULL) { struct socket *sp, *sp1; int wakeup; SOCK_LOCK(so); if (!SOLISTENING(so)) { SOCK_UNLOCK(so); return (EINVAL); } if (so->sol_accept_filter == NULL) { SOCK_UNLOCK(so); return (0); } if (so->sol_accept_filter->accf_destroy != NULL) so->sol_accept_filter->accf_destroy(so); if (so->sol_accept_filter_str != NULL) free(so->sol_accept_filter_str, M_ACCF); so->sol_accept_filter = NULL; so->sol_accept_filter_arg = NULL; so->sol_accept_filter_str = NULL; so->so_options &= ~SO_ACCEPTFILTER; /* * Move from incomplete queue to complete only those * connections, that are blocked by us. */ wakeup = 0; TAILQ_FOREACH_SAFE(sp, &so->sol_incomp, so_list, sp1) { SOCK_LOCK(sp); if (sp->so_options & SO_ACCEPTFILTER) { TAILQ_REMOVE(&so->sol_incomp, sp, so_list); TAILQ_INSERT_TAIL(&so->sol_comp, sp, so_list); sp->so_qstate = SQ_COMP; sp->so_options &= ~SO_ACCEPTFILTER; so->sol_incqlen--; so->sol_qlen++; wakeup = 1; } SOCK_UNLOCK(sp); } if (wakeup) solisten_wakeup(so); /* unlocks */ else SOLISTEN_UNLOCK(so); return (0); } /* * Pre-allocate any memory we may need later to avoid blocking at * untimely moments. This does not optimize for invalid arguments. */ afap = malloc(sizeof(*afap), M_TEMP, M_WAITOK); error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); afap->af_name[sizeof(afap->af_name)-1] = '\0'; afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; if (error) { free(afap, M_TEMP); return (error); } afp = accept_filt_get(afap->af_name); if (afp == NULL) { free(afap, M_TEMP); return (ENOENT); } if (afp->accf_create != NULL && afap->af_name[0] != '\0') { size_t len = strlen(afap->af_name) + 1; accept_filter_str = malloc(len, M_ACCF, M_WAITOK); strcpy(accept_filter_str, afap->af_name); } /* * Require a listen socket; don't try to replace an existing filter * without first removing it. */ SOCK_LOCK(so); - if (!SOLISTENING(so) || so->sol_accept_filter != NULL) { + if (__predict_false(!SOLISTENING(so))) { error = EINVAL; goto out; } + if (__predict_false(so->sol_accept_filter != NULL)) { + error = EBUSY; + goto out; + } /* * Invoke the accf_create() method of the filter if required. The * socket mutex is held over this call, so create methods for filters * can't block. */ if (afp->accf_create != NULL) { accept_filter_arg = afp->accf_create(so, afap->af_arg); if (accept_filter_arg == NULL) { error = EINVAL; goto out; } } so->sol_accept_filter = afp; so->sol_accept_filter_arg = accept_filter_arg; so->sol_accept_filter_str = accept_filter_str; accept_filter_str = NULL; so->so_options |= SO_ACCEPTFILTER; out: SOCK_UNLOCK(so); if (accept_filter_str != NULL) free(accept_filter_str, M_ACCF); free(afap, M_TEMP); return (error); } diff --git a/tests/sys/kern/socket_accf.c b/tests/sys/kern/socket_accf.c index 747bcda87010..ae6522397cf7 100644 --- a/tests/sys/kern/socket_accf.c +++ b/tests/sys/kern/socket_accf.c @@ -1,219 +1,251 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2022-2024 Gleb Smirnoff * * 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 AUTHOR 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 AUTHOR 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 #include #include #include #include #include static int listensock(struct sockaddr_in *sin) { int l; ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0); ATF_REQUIRE(fcntl(l, F_SETFL, O_NONBLOCK) != -1); ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, sizeof(int)) == 0); *sin = (struct sockaddr_in){ .sin_len = sizeof(sin), .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK), }; ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0); ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin, &(socklen_t){ sizeof(*sin) }) == 0); ATF_REQUIRE(listen(l, -1) == 0); return (l); } static int clientsock(struct sockaddr_in *sin) { int s; ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0); ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0); return (s); } static void accfon(int l, struct accept_filter_arg *af) { if (setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, af, sizeof(*af)) != 0) { if (errno == ENOENT) atf_tc_skip("Accept filter %s not loaded in kernel", af->af_name); else atf_tc_fail("setsockopt(SO_ACCEPTFILTER): %s", strerror(errno)); } } /* * XXX: return from send(2) on a localhost connection doesn't guarantee that * netisr has fully processed and delivered the data to the remote local * socket. Sleep a fraction of second to "guarantee" that it did. */ static ssize_t usend(int s, const void *msg, size_t len) { ssize_t rv; rv = send(s, msg, len, 0); usleep(100000); return (rv); } ATF_TC_WITHOUT_HEAD(data); ATF_TC_BODY(data, tc) { struct accept_filter_arg afa = { .af_name = "dataready" }; struct sockaddr_in sin; int l, s, a; l = listensock(&sin); accfon(l, &afa); s = clientsock(&sin); ATF_REQUIRE(accept(l, NULL, 0) == -1); ATF_REQUIRE(errno == EAGAIN); ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo")); ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); } ATF_TC_WITHOUT_HEAD(http); ATF_TC_BODY(http, tc) { struct accept_filter_arg afa = { .af_name = "httpready" }; struct sockaddr_in sin; int l, s, a; l = listensock(&sin); accfon(l, &afa); s = clientsock(&sin); /* 1) No data. */ ATF_REQUIRE(accept(l, NULL, 0) == -1); ATF_REQUIRE(errno == EAGAIN); /* 2) Data, that doesn't look like HTTP. */ ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo")); ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); close(s); close(a); #define CHUNK1 "GET / " #define CHUNK2 "HTTP/1.0\r\n\n" #define LEN(c) (sizeof(c) - 1) /* 3) Partial HTTP. */ s = clientsock(&sin); ATF_REQUIRE(usend(s, CHUNK1, LEN(CHUNK1)) == LEN(CHUNK1)); ATF_REQUIRE(accept(l, NULL, 0) == -1); ATF_REQUIRE(errno == EAGAIN); /* 4) Complete HTTP. */ ATF_REQUIRE(usend(s, CHUNK2, LEN(CHUNK2)) == LEN(CHUNK2)); ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); } ATF_TC_WITHOUT_HEAD(tls); ATF_TC_BODY(tls, tc) { struct accept_filter_arg afa = { .af_name = "tlsready" }; struct sockaddr_in sin; int l, s, a; l = listensock(&sin); accfon(l, &afa); s = clientsock(&sin); /* 1) No data. */ ATF_REQUIRE(accept(l, NULL, 0) == -1); ATF_REQUIRE(errno == EAGAIN); /* 2) Less than 5 bytes. */ ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo")); ATF_REQUIRE(errno == EAGAIN); /* 3) Something that doesn't look like TLS handshake. */ ATF_REQUIRE(usend(s, "bar", sizeof("bar")) == sizeof("bar")); ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); close(s); close(a); /* 4) Partial TLS record. */ s = clientsock(&sin); struct { uint8_t type; uint16_t version; uint16_t length; } __attribute__((__packed__)) header = { .type = 0x16, .length = htons((uint16_t)(arc4random() % 16384)), }; _Static_assert(sizeof(header) == 5, ""); ATF_REQUIRE(usend(s, &header, sizeof(header)) == sizeof(header)); ssize_t sent = 0; do { size_t len; char *buf; ATF_REQUIRE(accept(l, NULL, 0) == -1); ATF_REQUIRE(errno == EAGAIN); len = arc4random() % 1024; buf = alloca(len); ATF_REQUIRE(usend(s, buf, len) == (ssize_t)len); sent += len; } while (sent < ntohs(header.length)); /* TLS header with bytes >= declared length. */ ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); } +/* Check changing to a different filter. */ +ATF_TC_WITHOUT_HEAD(change); +ATF_TC_BODY(change, tc) +{ + struct accept_filter_arg dfa = { + .af_name = "dataready" + }; + struct accept_filter_arg hfa = { + .af_name = "httpready" + }; + struct sockaddr_in sin; + int n, l; + + l = listensock(&sin); + accfon(l, &dfa); + + /* Refuse to change filter without explicit removal of the old one. */ + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, &hfa, + sizeof(hfa)) != 0 && errno == EBUSY); + + /* But allow after clearing. */ + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0) == 0); + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, &hfa, + sizeof(hfa)) == 0); + + /* Must be listening socket. */ + ATF_REQUIRE((n = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(setsockopt(n, SOL_SOCKET, SO_ACCEPTFILTER, &dfa, + sizeof(dfa)) != 0 && errno == EINVAL); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, data); ATF_TP_ADD_TC(tp, http); ATF_TP_ADD_TC(tp, tls); + ATF_TP_ADD_TC(tp, change); return (atf_no_error()); }