From nobody Thu Nov 2 18:53:00 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SLtL460Z7z504t1; Thu, 2 Nov 2023 18:53:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SLtL45WKRz3bk0; Thu, 2 Nov 2023 18:53:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1698951180; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=syEOswIeUrSKj+qxfbyN+vWcx3oTAnFRBfR7rKVb3Q8=; b=JxOPtOeuGClGgGXITXH1W37Q2/DJwAuFncLB8gsMjcLPyDeKlnQTniIyXrDSvtq7lET4+D iAn0IDnGUjvT6Qj1lioGsmmX1lHk7yzxKnfzqEi4Ua177lInfikGOibHCKFbEkrSHjFGrG uySNZQ2Hwy7P0IxBsRr3ABlD0DUT47XT3fuK69qEp5fM2wXDmlHKvn0VOtjd7ym1A5L84l 0B7+MgX/CFbWOwByBhchEbYb7mnI4IoMlNWsbphc0YG/0Gkee4PdSqx3P5KFRXXD99JD0c T6AhyZ0PtufTYfhm91cFGC0/3z76mcyKpPjK3sIOoECXhdKqjDJjFlBkYQcdrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1698951180; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=syEOswIeUrSKj+qxfbyN+vWcx3oTAnFRBfR7rKVb3Q8=; b=gAUQPnsb7BGAzWZIQ0SXQ51OddBf08gjKdtrKwF7t4tDi5h/tOGcWXnkhxx+YRFmb/GBRJ xiu9vrnvmZXydka2cmW5Xk40rVry4aelMl91a0J5sqV0pV2J4VU/qkSfbxMTNVKp3EG2dr A2q9XA4jBGAZVkLgVNPAxn4gCCDZYMNrbouARZQp6+oqrL4n9pwikhgmv0B9yQgMwV3pgO XSeZd0PRUo1ioHXxLpsfi6v/nICca8l2FmcrrJU6XIptALGSw3UTp+6TnJXeOzKlTA+jLo 2MKXv0SirurMPKCY2/NSZjAFGDdRi6HRT8cCK3Q3fuj37j1UwjKVG5RGRoTelg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1698951180; a=rsa-sha256; cv=none; b=jghhZrLe7i5GnjP2QX5xfEMjuuwqhCcYU9fcIl4ZuZowi6XMW3rFqMdoBGnQlW+2M+buQy eQaL2HWuqmqbCeNBHVnPIfaIgtmE13i4NC8g04HMzZN7P70BBLROCexNbk40bDwtEvvHGl j+YGoZDOM+UgloYtI5zQjapIF44p2G5iYht3WWGq1P/vXoWPZvs7zhTXU4jjNkwpgUjnQO eHbzpAlpPYN/FmHPBf8qpuQbp/bLgdIFPV65A+J0/NGb2u4EUuCocvDSaiPBWmgFr8zTYl anVd1zLTX1W1r2vpTcd2vBzmStaZa7lEj0PThjUHx0L3Jl7PAdRDgRcv6ZhW6A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SLtL44YKlzhCq; Thu, 2 Nov 2023 18:53:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3A2Ir09J060940; Thu, 2 Nov 2023 18:53:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3A2Ir0V3060937; Thu, 2 Nov 2023 18:53:00 GMT (envelope-from git) Date: Thu, 2 Nov 2023 18:53:00 GMT Message-Id: <202311021853.3A2Ir0V3060937@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ed Maste Subject: git: b73612a34259 - main - blacklistd: Handle fds that are pointing to routing sockets List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b73612a34259a478c53eab408362d7bf879fefa6 Auto-Submitted: auto-generated The branch main has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=b73612a34259a478c53eab408362d7bf879fefa6 commit b73612a34259a478c53eab408362d7bf879fefa6 Author: Jose Luis Duran AuthorDate: 2022-10-12 16:14:44 +0000 Commit: Ed Maste CommitDate: 2023-11-02 18:52:40 +0000 blacklistd: Handle fds that are pointing to routing sockets If the fd has access to make changes via the routing socket, grant full permission to make filter changes. Obtained from: https://github.com/zoulasc/blocklist/commit/1b9475b2c8e0be2b9adc4d88e521ed488ac3c43c --- contrib/blacklist/bin/conf.c | 113 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 18 deletions(-) diff --git a/contrib/blacklist/bin/conf.c b/contrib/blacklist/bin/conf.c index 6eadf6b2ac8c..8f7e75a56be1 100644 --- a/contrib/blacklist/bin/conf.c +++ b/contrib/blacklist/bin/conf.c @@ -46,6 +46,7 @@ __RCSID("$NetBSD: conf.c,v 1.24 2016/04/04 15:52:56 christos Exp $"); #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ __RCSID("$NetBSD: conf.c,v 1.24 2016/04/04 15:52:56 christos Exp $"); #include #include #include +#include #include #include "bl.h" @@ -1000,32 +1002,72 @@ confset_match(const struct confset *cs, struct conf *c, return i; } -const struct conf * -conf_find(int fd, uid_t uid, const struct sockaddr_storage *rss, - struct conf *cr) +#ifdef AF_ROUTE +static int +conf_route_perm(int fd) { +#if defined(RTM_IFANNOUNCE) && defined(SA_SIZE) + /* + * Send a routing message that is not supported to check for access + * We expect EOPNOTSUPP for having access, since we are sending a + * request the system does not understand and EACCES if we don't have + * access. + */ + static struct sockaddr_in sin = { +#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN + .sin_len = sizeof(sin), +#endif + .sin_family = AF_INET, + }; + char buf[4096]; + struct rt_msghdr *rtm = (void *)buf; + char *cp = (char *)(rtm + 1); + size_t l; + +#define NEXTADDR(s) \ + l = SA_SIZE(sizeof(*s)); memmove(cp, s, l); cp += l; + memset(buf, 0, sizeof(buf)); + rtm->rtm_type = RTM_IFANNOUNCE; + rtm->rtm_flags = 0; + rtm->rtm_addrs = RTA_DST|RTA_GATEWAY; + rtm->rtm_version = RTM_VERSION; + rtm->rtm_seq = 666; + NEXTADDR(&sin); + NEXTADDR(&sin); + rtm->rtm_msglen = (u_short)((char *)cp - (char *)rtm); + if (write(fd, rtm, rtm->rtm_msglen) != -1) { + (*lfun)(LOG_ERR, "Writing to routing socket succeeded!"); + return 0; + } + switch (errno) { + case EACCES: + return 0; + case EOPNOTSUPP: + return 1; + default: + (*lfun)(LOG_ERR, + "Unexpected error writing to routing socket (%m)"); + return 0; + } +#else + return 0; +#endif +} +#endif + +static int +conf_handle_inet(int fd, const void *lss, struct conf *cr) { - int proto; - socklen_t slen; - struct sockaddr_storage lss; - size_t i; char buf[BUFSIZ]; + int proto; + socklen_t slen = sizeof(proto); - memset(cr, 0, sizeof(*cr)); - slen = sizeof(lss); - memset(&lss, 0, slen); - if (getsockname(fd, (void *)&lss, &slen) == -1) { - (*lfun)(LOG_ERR, "getsockname failed (%m)"); - return NULL; - } - - slen = sizeof(proto); if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &proto, &slen) == -1) { (*lfun)(LOG_ERR, "getsockopt failed (%m)"); - return NULL; + return -1; } if (debug) { - sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (void *)&lss); + sockaddr_snprintf(buf, sizeof(buf), "%a:%p", lss); (*lfun)(LOG_DEBUG, "listening socket: %s", buf); } @@ -1038,16 +1080,51 @@ conf_find(int fd, uid_t uid, const struct sockaddr_storage *rss, break; default: (*lfun)(LOG_ERR, "unsupported protocol %d", proto); + return -1; + } + return 0; +} + +const struct conf * +conf_find(int fd, uid_t uid, const struct sockaddr_storage *rss, + struct conf *cr) +{ + socklen_t slen; + struct sockaddr_storage lss; + size_t i; + char buf[BUFSIZ]; + + memset(cr, 0, sizeof(*cr)); + slen = sizeof(lss); + memset(&lss, 0, slen); + if (getsockname(fd, (void *)&lss, &slen) == -1) { + (*lfun)(LOG_ERR, "getsockname failed (%m)"); return NULL; } switch (lss.ss_family) { case AF_INET: cr->c_port = ntohs(((struct sockaddr_in *)&lss)->sin_port); + if (conf_handle_inet(fd, &lss, cr) == -1) + return NULL; break; case AF_INET6: cr->c_port = ntohs(((struct sockaddr_in6 *)&lss)->sin6_port); + if (conf_handle_inet(fd, &lss, cr) == -1) + return NULL; break; +#ifdef AF_ROUTE + case AF_ROUTE: + if (!conf_route_perm(fd)) { + (*lfun)(LOG_ERR, + "permission denied to routing socket (%m)"); + return NULL; + } + cr->c_proto = FSTAR; + cr->c_port = FSTAR; + memcpy(&lss, rss, sizeof(lss)); + break; +#endif default: (*lfun)(LOG_ERR, "unsupported family %d", lss.ss_family); return NULL;