From nobody Thu Aug 21 22:25:45 2025 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 4c7Hvw4rzlz64X5h; Thu, 21 Aug 2025 22:25:48 +0000 (UTC) (envelope-from cy.schubert@cschubert.com) Received: from omta003.cacentral1.a.cloudfilter.net (omta001.cacentral1.a.cloudfilter.net [3.97.99.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "Client", Issuer "CA" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4c7Hvw43bBz3DHZ; Thu, 21 Aug 2025 22:25:48 +0000 (UTC) (envelope-from cy.schubert@cschubert.com) Authentication-Results: mx1.freebsd.org; none Received: from shw-obgw-4001a.ext.cloudfilter.net ([10.228.9.142]) by cmsmtp with ESMTPS id p6XvukzlT9JM2pDj9u5z7c; Thu, 21 Aug 2025 22:25:47 +0000 Received: from spqr.komquats.com ([70.66.136.217]) by cmsmtp with ESMTPSA id pDj8us3jtWX70pDj9ulDuP; Thu, 21 Aug 2025 22:25:47 +0000 X-Auth-User: cschuber X-Authority-Analysis: v=2.4 cv=d71WygjE c=1 sm=1 tr=0 ts=68a79ceb a=h7br+8Ma+Xn9xscxy5znUg==:117 a=h7br+8Ma+Xn9xscxy5znUg==:17 a=IkcTkHD0fZMA:10 a=2OwXVqhp2XgA:10 a=6I5d2MoRAAAA:8 a=EkcXrb_YAAAA:8 a=YxBL1-UpAAAA:8 a=NynCIDqhjON2MYOQ2PoA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=LK5xJRSDVpKd5WXXoEvA:22 a=Ia-lj3WSrqcvXOmTRaiG:22 Received: from slippy.cwsent.com (slippy.cwsent.com [10.1.1.91]) by spqr.komquats.com (Postfix) with ESMTP id A48CBED; Thu, 21 Aug 2025 22:25:45 +0000 (UTC) Received: by slippy.cwsent.com (Postfix, from userid 1000) id 9AE66380; Thu, 21 Aug 2025 22:25:45 +0000 (UTC) X-Mailer: exmh version 2.9.0 11/07/2018 with nmh-1.8+dev Reply-to: Cy Schubert From: Cy Schubert X-os: FreeBSD X-Sender: cy@cwsent.com X-URL: http://www.cschubert.com/ To: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= cc: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: Re: git: b6ea2513f776 - main - tzcode: Limit TZ for setugid programs In-reply-to: <202508211859.57LIxlcm003786@gitrepo.freebsd.org> References: <202508211859.57LIxlcm003786@gitrepo.freebsd.org> Comments: In-reply-to Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= message dated "Thu, 21 Aug 2025 18:59:47 +0000." 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Date: Thu, 21 Aug 2025 15:25:45 -0700 Message-Id: <20250821222545.9AE66380@slippy.cwsent.com> X-CMAE-Envelope: MS4xfLm/7R/GKTWTTfbuzj4y4qmQyr9rwq78zzua2tyKECyVxIPjrOEa0CMaPj16Fb6kbT+cAp5sPLReoJE328ocrtEApTQf/uIJF/+ecd2Tu/bVoSv6Yz2O ABIb0rbjTlAHtmEjUbaChrkBBjJdhPeHATAGVUwSkfb90XzRwV+9c4GUOoVTqgNF+HUko/RigVyEgWdtpUR5nmJuwuQmrncw41y5KP5/TycSyXKp7eTGe3xL S2bpn+NqJBuDEzcvEcJ3TXPdd+PnKqVepneh+N6Et7xxe5VvUSvGF3qpUSPKmemb34l8uheCk8AlgU93psDNkHWCpjac2c9w10hNjd9imAc= X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:16509, ipnet:3.96.0.0/15, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: 4c7Hvw43bBz3DHZ In message <202508211859.57LIxlcm003786@gitrepo.freebsd.org>, Dag-Erling =?utf- 8?Q?Sm=C3=B8rgrav?= writes: > The branch main has been updated by des: > > URL: https://cgit.FreeBSD.org/src/commit/?id=b6ea2513f7769ea9d9e4d342777111ad > d2c903b0 > > commit b6ea2513f7769ea9d9e4d342777111add2c903b0 > Author: Dag-Erling Smørgrav > AuthorDate: 2025-08-21 16:34:32 +0000 > Commit: Dag-Erling Smørgrav > CommitDate: 2025-08-21 18:59:37 +0000 > > tzcode: Limit TZ for setugid programs > > The zoneinfo parser can be told to read any file the program can access > by setting TZ to either an absolute path, or a path relative to the > zoneinfo directory. For setugid programs, we previously had a hack from > OpenBSD which rejects values of TZ deemed unsafe, but that was rather > arbitrary (anything containing a dot, for instance). Leverage openat() > with AT_RESOLVE_BENEATH instead. > > For simplicity, move the TZ change detection code to after we've opened > the file, and stat the file descriptor rather than the name. > > Reviewed by: jhb > Differential Revision: https://reviews.freebsd.org/D52029 > --- > contrib/tzcode/localtime.c | 70 +++++++++++++++++++++++++++++--------------- > -- > 1 file changed, 45 insertions(+), 25 deletions(-) > > diff --git a/contrib/tzcode/localtime.c b/contrib/tzcode/localtime.c > index 6eabe0afe570..1a01db931cab 100644 > --- a/contrib/tzcode/localtime.c > +++ b/contrib/tzcode/localtime.c > @@ -408,13 +408,13 @@ scrub_abbrs(struct state *sp) > * 1 if the time zone has changed > */ > static int > -tzfile_changed(const char *name) > +tzfile_changed(const char *name, int fd) > { > static char old_name[PATH_MAX]; > static struct stat old_sb; > struct stat sb; > > - if (stat(name, &sb) != 0) > + if (_fstat(fd, &sb) != 0) > return -1; > > if (strcmp(name, old_name) != 0) { > @@ -446,8 +446,10 @@ union input_buffer { > + 4 * TZ_MAX_TIMES]; > }; > > +#ifndef __FreeBSD__ > /* TZDIR with a trailing '/' rather than a trailing '\0'. */ > static char const tzdirslash[sizeof TZDIR] = TZDIR "/"; > +#endif /* !__FreeBSD__ */ > > /* Local storage needed for 'tzloadbody'. */ > union local_storage { > @@ -460,6 +462,7 @@ union local_storage { > struct state st; > } u; > > +#ifndef __FreeBSD__ > /* The name of the file to be opened. Ideally this would have no > size limits, to support arbitrarily long Zone names. > Limiting Zone names to 1024 bytes should suffice for practical use. > @@ -467,6 +470,7 @@ union local_storage { > file_analysis as that struct is allocated anyway, as the other > union member. */ > char fullname[max(sizeof(struct file_analysis), sizeof tzdirslash + 1024)] > ; > +#endif /* !__FreeBSD__ */ > }; > > /* Load tz data from the file named NAME into *SP. Read extended > @@ -480,7 +484,11 @@ tzloadbody(char const *name, struct state *sp, bool doex > tend, > register int fid; > register int stored; > register ssize_t nread; > +#ifdef __FreeBSD__ > + int serrno; > +#else /* !__FreeBSD__ */ > register bool doaccess; > +#endif /* !__FreeBSD__ */ > register union input_buffer *up = &lsp->u.u; > register int tzheadsize = sizeof(struct tzhead); > > @@ -494,6 +502,7 @@ tzloadbody(char const *name, struct state *sp, bool doext > end, > > if (name[0] == ':') > ++name; > +#ifndef __FreeBSD__ > #ifdef SUPPRESS_TZDIR > /* Do not prepend TZDIR. This is intended for specialized > applications only, due to its security implications. */ > @@ -502,9 +511,7 @@ tzloadbody(char const *name, struct state *sp, bool doext > end, > doaccess = name[0] == '/'; > #endif > if (!doaccess) { > -#ifndef __FreeBSD__ > char const *dot; > -#endif /* !__FreeBSD__ */ > if (sizeof lsp->fullname - sizeof tzdirslash <= strlen(name)) > return ENAMETOOLONG; > > @@ -514,7 +521,6 @@ tzloadbody(char const *name, struct state *sp, bool doext > end, > memcpy(lsp->fullname, tzdirslash, sizeof tzdirslash); > strcpy(lsp->fullname + sizeof tzdirslash, name); > > -#ifndef __FreeBSD__ > /* Set doaccess if NAME contains a ".." file name > component, as such a name could read a file outside > the TZDIR virtual subtree. */ > @@ -524,35 +530,49 @@ tzloadbody(char const *name, struct state *sp, bool doe > xtend, > doaccess = true; > break; > } > -#endif /* !__FreeBSD__ */ > > name = lsp->fullname; > } > -#ifndef __FreeBSD__ > if (doaccess && access(name, R_OK) != 0) > return errno; > -#endif /* !__FreeBSD__ */ > -#ifdef DETECT_TZ_CHANGES > - if (doextend) { > - /* > - * Detect if the timezone file has changed. Check > - * 'doextend' to ignore TZDEFRULES; the tzfile_changed() > - * function can only keep state for a single file. > - */ > - switch (tzfile_changed(name)) { > - case -1: > - return errno; > - case 0: > - return 0; > - case 1: > - break; > - } > - } > -#endif /* DETECT_TZ_CHANGES */ > +#else /* __FreeBSD__ */ > + if (issetugid()) { > + const char *relname = name; > + if (strncmp(relname, TZDIR "/", strlen(TZDIR) + 1) == 0) > + relname += strlen(TZDIR) + 1; > + int dd = _open(TZDIR, O_DIRECTORY | O_RDONLY); > + if (dd < 0) > + return errno; > + fid = _openat(dd, relname, O_RDONLY | O_BINARY, AT_RESOLVE_BENEATH > ); > + serrno = errno; > + _close(dd); > + errno = serrno; > + } else > +#endif > fid = _open(name, O_RDONLY | O_BINARY); > if (fid < 0) > return errno; > > +#ifdef DETECT_TZ_CHANGES > + if (doextend) { > + /* > + * Detect if the timezone file has changed. Check 'doextend' to > + * ignore TZDEFRULES; the tzfile_changed() function can only > + * keep state for a single file. > + */ > + switch (tzfile_changed(name, fid)) { > + case -1: > + serrno = errno; > + _close(fid); > + return serrno; > + case 0: > + _close(fid); > + return 0; > + case 1: > + break; > + } > + } > +#endif /* DETECT_TZ_CHANGES */ > nread = _read(fid, up->buf, sizeof up->buf); > if (nread < tzheadsize) { > int err = nread < 0 ? errno : EINVAL; > Further testing has shown that this commit has TZ America/Vancouver to UTC. Setting TZ to PST8PDT still works but TZ=America/Vancouver does not. -- Cheers, Cy Schubert FreeBSD UNIX: Web: https://FreeBSD.org NTP: Web: https://nwtime.org e**(i*pi)+1=0