From d83f781dc0c85db1fa8d74b7d9226195fd41b8c8 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 25 Oct 2019 14:24:35 +0200 Subject: [PATCH] Add OpenBSD strlcat() and strlcpy() safe string manipulation APIs Signed-off-by: Joachim Nilsson --- configure.ac | 5 +++++ lib/.gitignore | 2 ++ lib/strlcat.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/strlcpy.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 8 ++++++- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 lib/.gitignore create mode 100644 lib/strlcat.c create mode 100644 lib/strlcpy.c diff --git a/configure.ac b/configure.ac index 77f3d52..792bed5 100644 --- a/configure.ac +++ b/configure.ac @@ -32,6 +32,11 @@ AC_HEADER_STDC # Check for required packages PKG_PROG_PKG_CONFIG +# Check for usually missing API's, which we can replace +AC_REPLACE_FUNCS([strlcpy strlcat]) +AC_CONFIG_LIBOBJ_DIR([lib]) + +# Command line options AC_ARG_WITH(suspend-time, AS_HELP_STRING([--with-suspend-time=SEC], [Retry timeout for remote syslogd servers, default: 180]), [suspend_time=$withval], [suspend_time='no']) diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..824d581 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1,2 @@ +*.o +.dirstamp diff --git a/lib/strlcat.c b/lib/strlcat.c new file mode 100644 index 0000000..14c53a1 --- /dev/null +++ b/lib/strlcat.c @@ -0,0 +1,55 @@ +/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller + * + * 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. + */ + +#include +#include + +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} diff --git a/lib/strlcpy.c b/lib/strlcpy.c new file mode 100644 index 0000000..e9a7fe4 --- /dev/null +++ b/lib/strlcpy.c @@ -0,0 +1,50 @@ +/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller + * + * 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. + */ + +#include +#include + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} diff --git a/src/Makefile.am b/src/Makefile.am index 31493c0..d05003e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +AUTOMAKE_OPTIONS = subdir-objects bin_PROGRAMS = logger sbin_PROGRAMS = syslogd klogd @@ -26,16 +27,21 @@ AM_CFLAGS += -fno-strict-aliasing -fno-strength-reduce syslogd_SOURCES = syslogd.c pidfile.c pidfile.h syslogd_CPPFLAGS = -D_BSD_SOURCE -D_DEFAULT_SOURCE +syslogd_LDADD = $(LIBS) $(LIBOBJS) klogd_SOURCES = klogd.c klogd.h syslog.c pidfile.c pidfile.h \ ksym.c ksyms.h ksym_mod.c module.h klogd_CPPFLAGS = -DALLOW_KERNEL_LOGGING -D_BSD_SOURCE -D_DEFAULT_SOURCE +klogd_LDADD = $(LIBS) $(LIBOBJS) logger_SOURCES = logger.c syslog.c logger_CPPFLAGS = -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE -D_DEFAULT_SOURCE +logger_LDADD = $(LIBS) $(LIBOBJS) tsyslogd_SOURCES = $(syslogd_SOURCES) tsyslogd_CPPFLAGS = $(syslogd_CPPFLAGS) -DTESTING +tsyslogd_LDADD = $(LIBS) $(LIBOBJS) -syslog_tst_SOURCES = syslog_tst.c syslog.c +syslog_tst_SOURCES = syslog_tst.c syslog.c syslog_tst_CPPFLAGS = -D_BSD_SOURCE -D_DEFAULT_SOURCE -DTESTING +syslog_tst_LDADD = $(LIBS) $(LIBOBJS)