diff --git a/lib/libc/gen/__getosreldate.c b/lib/libc/gen/__getosreldate.c index 81fd125b2ee6..685382f76977 100644 --- a/lib/libc/gen/__getosreldate.c +++ b/lib/libc/gen/__getosreldate.c @@ -1,70 +1,69 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2007 Peter Wemm - * 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include "libc_private.h" /* * This is private to libc. It is intended for wrapping syscall stubs in order * to avoid having to put SIGSYS signal handlers in place to test for presence * of new syscalls. This caches the result in order to be as quick as possible. * * Use getosreldate(3) for public use as it respects the $OSVERSION environment * variable. */ int __getosreldate(void) { static int osreldate; size_t len; int oid[2]; int error, osrel; if (osreldate != 0) return (osreldate); error = _elf_aux_info(AT_OSRELDATE, &osreldate, sizeof(osreldate)); if (error == 0 && osreldate != 0) return (osreldate); oid[0] = CTL_KERN; oid[1] = KERN_OSRELDATE; osrel = 0; len = sizeof(osrel); error = sysctl(oid, 2, &osrel, &len, NULL, 0); if (error == 0 && osrel > 0 && len == sizeof(osrel)) osreldate = osrel; return (osreldate); } diff --git a/lib/libc/gen/setproctitle.3 b/lib/libc/gen/setproctitle.3 index a725977e5191..15bafa23a982 100644 --- a/lib/libc/gen/setproctitle.3 +++ b/lib/libc/gen/setproctitle.3 @@ -1,134 +1,139 @@ -.\" Copyright (c) 1995 Peter Wemm -.\" All rights reserved. +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 1995 Peter Wemm .\" .\" Redistribution and use in source and binary forms, with or without -.\" modification, is permitted provided that the following conditions +.\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright -.\" notice immediately at the beginning of the file, without modification, -.\" this list of conditions, and the following disclaimer. +.\" 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. -.\" 3. This work was done expressly for inclusion into FreeBSD. Other use -.\" is permitted provided this notation is included. -.\" 4. Absolutely no warranty of function or purpose is made by the author -.\" Peter Wemm. -.\" 5. Modifications may be freely made to this file providing the above -.\" conditions are met. .\" -.\" $FreeBSD$ +.\" 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. .\" .\" The following requests are required for all man pages. .Dd November 28, 2022 .Dt SETPROCTITLE 3 .Os .Sh NAME .Nm setproctitle .Nm setproctitle_fast .Nd set process title .Sh SYNOPSIS .In unistd.h .Ft void .Fn setproctitle "const char *fmt" "..." .Ft void .Fn setproctitle_fast "const char *fmt" "..." .Sh DESCRIPTION The .Fn setproctitle library routine sets the process title that appears on the .Xr ps 1 command. The .Fn setproctitle_fast variant is optimized for high frequency updates, but may make the .Xr ps 1 command slightly slower by not updating the kernel cache of the program arguments. .Pp The title is set from the executable's name, followed by the result of a .Xr printf 3 style expansion of the arguments as specified by the .Va fmt argument. If the .Va fmt argument begins with a .Dq - character, the executable's name is skipped. .Pp If .Va fmt is NULL, the process title is restored. .Sh EXAMPLES To set the title on a daemon to indicate its activity: .Bd -literal -offset indent setproctitle("talking to %s", inet_ntoa(addr)); .Ed .Sh SEE ALSO .Xr ps 1 , .Xr w 1 , .Xr setprogname 3 , .Xr kvm 3 , .Xr kvm_getargv 3 , .Xr printf 3 .Sh STANDARDS The .Fn setproctitle function is implicitly non-standard. Other methods of causing the .Xr ps 1 command line to change, including copying over the argv[0] string are also implicitly non-portable. It is preferable to use an operating system supplied .Fn setproctitle if present. .Pp Unfortunately, it is possible that there are other calling conventions to other versions of .Fn setproctitle , although none have been found by the author as yet. This is believed to be the predominant convention. .Pp It is thought that the implementation is compatible with other systems, including .Nx and .Bsx . .Sh HISTORY The .Fn setproctitle function first appeared in .Fx 2.2 . The .Fn setproctitle_fast function first appeared in .Fx 12 . Other operating systems have similar functions. .Sh AUTHORS .An -nosplit .An Peter Wemm Aq Mt peter@FreeBSD.org stole the idea from the .Sy "Sendmail 8.7.3" source code by .An Eric Allman Aq Mt eric@sendmail.org . .Sh BUGS Never pass a string with user-supplied data as a format without using .Ql %s . An attacker can put format specifiers in the string to mangle your stack, leading to a possible security hole. This holds true even if the string was built using a function like .Fn snprintf , as the resulting string may still contain user-supplied conversion specifiers for later interpolation by .Fn setproctitle . .Pp Always use the proper secure idiom: .Pp .Dl setproctitle("%s", string); diff --git a/lib/libc/gen/setproctitle.c b/lib/libc/gen/setproctitle.c index 08612e99d9bb..743f27d15b50 100644 --- a/lib/libc/gen/setproctitle.c +++ b/lib/libc/gen/setproctitle.c @@ -1,211 +1,221 @@ -/* - * Copyright (c) 1995 Peter Wemm - * All rights reserved. +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 1995 Peter Wemm * * Redistribution and use in source and binary forms, with or without - * modification, is permitted provided that the following conditions + * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice immediately at the beginning of the file, without modification, - * this list of conditions, and the following disclaimer. + * 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. - * 3. Absolutely no warranty of function or purpose is made by the author - * Peter Wemm. + * + * 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 __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "libc_private.h" /* * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and * in different locations. * 1: old_ps_strings at the very top of the stack. * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. * 3: ps_strings at the very top of the stack. * We only support a kernel providing #3 style ps_strings. * * For historical purposes, a definition of the old ps_strings structure * and location is preserved below: struct old_ps_strings { char *old_ps_argvstr; int old_ps_nargvstr; char *old_ps_envstr; int old_ps_nenvstr; }; #define OLD_PS_STRINGS ((struct old_ps_strings *) \ (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) */ #include #define SPT_BUFSIZE 2048 /* from other parts of sendmail */ static char * setproctitle_internal(const char *fmt, va_list ap) { static struct ps_strings *ps_strings; static char *buf = NULL; static char *obuf = NULL; static char **oargv; static int oargc = -1; static char *nargv[2] = { NULL, NULL }; char **nargvp; int nargc; int i; size_t len; unsigned long ul_ps_strings; if (buf == NULL) { buf = malloc(SPT_BUFSIZE); if (buf == NULL) return (NULL); nargv[0] = buf; } if (obuf == NULL ) { obuf = malloc(SPT_BUFSIZE); if (obuf == NULL) return (NULL); *obuf = '\0'; } if (fmt) { buf[SPT_BUFSIZE - 1] = '\0'; if (fmt[0] == '-') { /* skip program name prefix */ fmt++; len = 0; } else { /* print program name heading for grep */ (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname()); len = strlen(buf); } /* print the argument string */ (void)vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap); nargvp = nargv; nargc = 1; } else if (*obuf != '\0') { /* Idea from NetBSD - reset the title on fmt == NULL */ nargvp = oargv; nargc = oargc; } else /* Nothing to restore */ return (NULL); if (ps_strings == NULL) (void)_elf_aux_info(AT_PS_STRINGS, &ps_strings, sizeof(ps_strings)); if (ps_strings == NULL) { len = sizeof(ul_ps_strings); if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 0) == -1) return (NULL); ps_strings = (struct ps_strings *)ul_ps_strings; } if (ps_strings == NULL) return (NULL); /* * PS_STRINGS points to zeroed memory on a style #2 kernel. * Should not happen. */ if (ps_strings->ps_argvstr == NULL) return (NULL); /* style #3 */ if (oargc == -1) { /* Record our original args */ oargc = ps_strings->ps_nargvstr; oargv = ps_strings->ps_argvstr; for (i = len = 0; i < oargc; i++) { /* * The program may have scribbled into its * argv array, e.g., to remove some arguments. * If that has happened, break out before * trying to call strlen on a NULL pointer. */ if (oargv[i] == NULL) { oargc = i; break; } snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", len != 0 ? " " : "", oargv[i]); if (len != 0) len++; len += strlen(oargv[i]); if (len >= SPT_BUFSIZE) break; } } ps_strings->ps_nargvstr = nargc; ps_strings->ps_argvstr = nargvp; return (nargvp[0]); } static int fast_update = 0; void setproctitle_fast(const char *fmt, ...) { va_list ap; char *buf; int oid[4]; va_start(ap, fmt); buf = setproctitle_internal(fmt, ap); va_end(ap); if (buf && !fast_update) { /* Tell the kernel to start looking in user-space */ oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; oid[3] = -1; sysctl(oid, 4, 0, 0, "", 0); fast_update = 1; } } void setproctitle(const char *fmt, ...) { va_list ap; char *buf; int oid[4]; va_start(ap, fmt); buf = setproctitle_internal(fmt, ap); va_end(ap); if (buf != NULL) { /* Set the title into the kernel cached command line */ oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; oid[3] = -1; sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1); fast_update = 0; } }