From f499cd524a0c5bcb7dce453bda93aba09cc3af0d Mon Sep 17 00:00:00 2001 From: christos Date: Wed, 8 Nov 2006 19:52:11 +0000 Subject: [PATCH] Add a BSD-licensed re-implementation of the gcc-4.1 libssp. This adds bounds checking for the following functions: memset, fgets, gets, memcpy, memmove, memset, snprintf, sprintf, strcat, strcpy, strncat, strncpy, vsnprintf, vsprintf This functionality is enabled by compiling with: gcc -I/usr/include/ssp -D_FORTIFY_SOURCE=<1|2> -lssp Eventually gcc will provide -ffortify-source or something to automate this [we hope :-)] --- lib/libssp/Makefile | 17 ++++++ lib/libssp/fgets_chk.c | 57 ++++++++++++++++++++ lib/libssp/gets_chk.c | 74 ++++++++++++++++++++++++++ lib/libssp/memcpy_chk.c | 51 ++++++++++++++++++ lib/libssp/memmove_chk.c | 51 ++++++++++++++++++ lib/libssp/memset_chk.c | 50 ++++++++++++++++++ lib/libssp/snprintf_chk.c | 61 ++++++++++++++++++++++ lib/libssp/sprintf_chk.c | 64 +++++++++++++++++++++++ lib/libssp/ssp/ssp.h | 74 ++++++++++++++++++++++++++ lib/libssp/ssp/stdio.h | 79 ++++++++++++++++++++++++++++ lib/libssp/ssp/string.h | 104 +++++++++++++++++++++++++++++++++++++ lib/libssp/ssp/unistd.h | 63 ++++++++++++++++++++++ lib/libssp/strcat_chk.c | 65 +++++++++++++++++++++++ lib/libssp/strcpy_chk.c | 53 +++++++++++++++++++ lib/libssp/strncat_chk.c | 75 ++++++++++++++++++++++++++ lib/libssp/strncpy_chk.c | 52 +++++++++++++++++++ lib/libssp/vsnprintf_chk.c | 53 +++++++++++++++++++ lib/libssp/vsprintf_chk.c | 61 ++++++++++++++++++++++ 18 files changed, 1104 insertions(+) create mode 100644 lib/libssp/Makefile create mode 100644 lib/libssp/fgets_chk.c create mode 100644 lib/libssp/gets_chk.c create mode 100644 lib/libssp/memcpy_chk.c create mode 100644 lib/libssp/memmove_chk.c create mode 100644 lib/libssp/memset_chk.c create mode 100644 lib/libssp/snprintf_chk.c create mode 100644 lib/libssp/sprintf_chk.c create mode 100644 lib/libssp/ssp/ssp.h create mode 100644 lib/libssp/ssp/stdio.h create mode 100644 lib/libssp/ssp/string.h create mode 100644 lib/libssp/ssp/unistd.h create mode 100644 lib/libssp/strcat_chk.c create mode 100644 lib/libssp/strcpy_chk.c create mode 100644 lib/libssp/strncat_chk.c create mode 100644 lib/libssp/strncpy_chk.c create mode 100644 lib/libssp/vsnprintf_chk.c create mode 100644 lib/libssp/vsprintf_chk.c diff --git a/lib/libssp/Makefile b/lib/libssp/Makefile new file mode 100644 index 000000000000..4e018d405b9a --- /dev/null +++ b/lib/libssp/Makefile @@ -0,0 +1,17 @@ +# $NetBSD: Makefile,v 1.1 2006/11/08 19:52:11 christos Exp $ + +USE_SHLIBDIR= yes +WARNS=4 + +CPPFLAGS+=-I${.CURDIR}/ssp +LIB= ssp +SRCS= gets_chk.c fgets_chk.c memcpy_chk.c memmove_chk.c memset_chk.c \ + snprintf_chk.c sprintf_chk.c strcat_chk.c strcpy_chk.c \ + strncat_chk.c strncpy_chk.c vsnprintf_chk.c vsprintf_chk.c + +INCS= ssp/ssp.h ssp/stdio.h ssp/string.h ssp/unistd.h +INCSDIR=/usr/include + +.include +.include +.include diff --git a/lib/libssp/fgets_chk.c b/lib/libssp/fgets_chk.c new file mode 100644 index 000000000000..0835f8b75077 --- /dev/null +++ b/lib/libssp/fgets_chk.c @@ -0,0 +1,57 @@ +/* $NetBSD: fgets_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: fgets_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include +#include +#include + +char * +__fgets_chk(char * __restrict buf, int len, size_t slen, FILE *fp) +{ + if (slen >= (size_t)INT_MAX) + return fgets(buf, len, fp); + + if (len >= 0 && len > slen) + __chk_fail(); + + return fgets(buf, len, fp); +} diff --git a/lib/libssp/gets_chk.c b/lib/libssp/gets_chk.c new file mode 100644 index 000000000000..eda0cfe69ef7 --- /dev/null +++ b/lib/libssp/gets_chk.c @@ -0,0 +1,74 @@ +/* $NetBSD: gets_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: gets_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include +#include +#include + +char * +__gets_chk(char * __restrict buf, size_t slen) +{ + char *abuf; + size_t len; + + if (slen >= (size_t)INT_MAX) + return gets(buf); + + if ((abuf = malloc(slen + 1)) == NULL) + return gets(buf); + + if (fgets(abuf, (int)(slen + 1), stdin) == NULL) + return NULL; + + len = strlen(abuf); + if (len > 0 && abuf[len - 1] == '\n') + --len; + + if (len >= slen) + __chk_fail(); + + (void)memcpy(buf, abuf, len); + + buf[len] = '\0'; + free(abuf); + return buf; +} diff --git a/lib/libssp/memcpy_chk.c b/lib/libssp/memcpy_chk.c new file mode 100644 index 000000000000..070de7606f07 --- /dev/null +++ b/lib/libssp/memcpy_chk.c @@ -0,0 +1,51 @@ +/* $NetBSD: memcpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: memcpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +void * +__memcpy_chk(void * __restrict dst, const void * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + return memcpy(dst, src, len); +} diff --git a/lib/libssp/memmove_chk.c b/lib/libssp/memmove_chk.c new file mode 100644 index 000000000000..c2faa2160471 --- /dev/null +++ b/lib/libssp/memmove_chk.c @@ -0,0 +1,51 @@ +/* $NetBSD: memmove_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: memmove_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +void * +__memmove_chk(void *dst, void *src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + return memmove(dst, src, len); +} diff --git a/lib/libssp/memset_chk.c b/lib/libssp/memset_chk.c new file mode 100644 index 000000000000..8011d4b3bfee --- /dev/null +++ b/lib/libssp/memset_chk.c @@ -0,0 +1,50 @@ +/* $NetBSD: memset_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: memset_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +void * +__memset_chk(void * __restrict dst, int val, size_t len, size_t slen) +{ + if (len > slen) + __chk_fail(); + return memset(dst, val, len); +} diff --git a/lib/libssp/snprintf_chk.c b/lib/libssp/snprintf_chk.c new file mode 100644 index 000000000000..551e6443aa34 --- /dev/null +++ b/lib/libssp/snprintf_chk.c @@ -0,0 +1,61 @@ +/* $NetBSD: snprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: snprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include + +/*ARGSUSED*/ +int +__snprintf_chk(char * __restrict buf, size_t len, int flags, size_t slen, + const char * __restrict fmt, ...) +{ + va_list ap; + int rv; + + if (len > slen) + __chk_fail(); + + va_start(ap, fmt); + rv = vsnprintf(buf, len, fmt, ap); + va_end(ap); + + return rv; +} diff --git a/lib/libssp/sprintf_chk.c b/lib/libssp/sprintf_chk.c new file mode 100644 index 000000000000..401dca305b9c --- /dev/null +++ b/lib/libssp/sprintf_chk.c @@ -0,0 +1,64 @@ +/* $NetBSD: sprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: sprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include +#include + +int +/*ARGSUSED*/ +__sprintf_chk(char * __restrict buf, int flags, size_t slen, + const char * __restrict fmt, ...) +{ + va_list ap; + int rv; + + va_start(ap, fmt); + if (slen > (size_t)INT_MAX) + rv = vsprintf(buf, fmt, ap); + else { + if ((rv = vsnprintf(buf, slen, fmt, ap)) >= 0 && rv >= slen) + __chk_fail(); + } + va_end(ap); + + return rv; +} diff --git a/lib/libssp/ssp/ssp.h b/lib/libssp/ssp/ssp.h new file mode 100644 index 000000000000..219f3d0899e7 --- /dev/null +++ b/lib/libssp/ssp/ssp.h @@ -0,0 +1,74 @@ +/* $NetBSD: ssp.h,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ +#ifndef _SSP_SSP_H_ +#define _SSP_SSP_H_ + +#include + +#if !defined(__cplusplus) +# if _FORTIFY_SOURCE > 0 && __OPTIMIZE__ > 0 && __GNUC_PREREQ__(4, 1) +# if _FORTIFY_SOURCE > 1 +# define __SSP_FORTIFY_LEVEL 2 +# else +# define __SSP_FORTIFY_LEVEL 1 +# endif +# endif +#endif + +#define __ssp_bos(ptr) __builtin_object_size(ptr, __SSP_FORTIFY_LEVEL > 1) +#define __ssp_bos0(ptr) __builtin_object_size(ptr, 0) +#define __ssp_redirect_raw(rtype, fun, args, call, bos) \ + inline __attribute__((__always_inline__)) rtype __ ## fun ## _alias args; \ + inline __attribute__((__always_inline__)) rtype \ + __ ## fun ## _alias args { \ + if (bos(__buf) != (size_t)-1 && __len > bos(__buf)) \ + __chk_fail(); \ + return fun call; \ + } + +#define __ssp_redirect(rtype, fun, args, call) \ + __ssp_redirect_raw(rtype, fun, args, call, __ssp_bos) +#define __ssp_redirect0(rtype, fun, args, call) \ + __ssp_redirect_raw(rtype, fun, args, call, __ssp_bos0) + +__BEGIN_DECLS +void __stack_chk_fail(void) __attribute__((__noreturn__)); +void __chk_fail(void) __attribute__((__noreturn__)); +__END_DECLS + +#endif /* _SSP_SSP_H_ */ diff --git a/lib/libssp/ssp/stdio.h b/lib/libssp/ssp/stdio.h new file mode 100644 index 000000000000..a3fece35dec6 --- /dev/null +++ b/lib/libssp/ssp/stdio.h @@ -0,0 +1,79 @@ +/* $NetBSD: stdio.h,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ +#ifndef _SSP_STDIO_H_ +#define _SSP_STDIO_H_ + +#include +#include_next + +__BEGIN_DECLS +int __sprintf_chk(char *__restrict, int, size_t, const char *__restrict, ...) + __attribute__((__format__(__printf__, 4, 5))); +int __vsprintf_chk(char *__restrict, int, size_t, const char *__restrict, + _BSD_VA_LIST_) __attribute__((__format__(__printf__, 4, 0))); +int __snprintf_chk(char *__restrict, size_t, int, size_t, + const char *__restrict, ...) __attribute__((__format__(__printf__, 5, 6))); +int __vnsprintf_chk(char *__restrict, int, size_t, const char *__restrict, + _BSD_VA_LIST_) __attribute__((__format__(__printf__, 5, 0))); +char *__gets_chk(char *, size_t); +char *__fgets_chk(char *, int, size_t, FILE *); +__END_DECLS + +#if __SSP_FORTIFY_LEVEL > 0 + + +#define sprintf(str, ...) \ + __builtin___sprintf_chk(str, 0, __ssp_bos(str), __VA_ARGS__) + +#define vsprintf(str, fmt, ap) \ + __builtin___vsprintf_chk(str, 0, __ssp_bos(str), fmt, ap) + +#define snprintf(str, len, ...) \ + __builtin___snprintf_chk(str, len, 0, __ssp_bos(str), __VA_ARGS__) + +#define vsnprintf(str, len, fmt, ap) \ + __builtin___vsnprintf_chk(str, len, 0, __ssp_bos(str), fmt, ap) + +#define gets(str) \ + __gets_chk(str, __ssp_bos(str)) + +#define fgets(str, len, fp) \ + __fgets_chk(str, len, __ssp_bos(str), fp) +#endif /* __SSP_FORTIFY_LEVEL > 0 */ + +#endif /* _SSP_STDIO_H_ */ diff --git a/lib/libssp/ssp/string.h b/lib/libssp/ssp/string.h new file mode 100644 index 000000000000..9d06fa64bd11 --- /dev/null +++ b/lib/libssp/ssp/string.h @@ -0,0 +1,104 @@ +/* $NetBSD: string.h,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ +#ifndef _SSP_STRING_H_ +#define _SSP_STRING_H_ + +#include +#include_next + +#if __SSP_FORTIFY_LEVEL > 0 + +#define __ssp_bos_check3(fun, dst, src, len) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)) : \ + __ ## fun ## _ichk(dst, src, len)) + +#define __ssp_bos_check2(fun, dst, src) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)) : \ + __ ## fun ## _ichk(dst, src)) + +#define __ssp_bos_icheck3_restrict(fun, type1, type2) \ +type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \ +inline __attribute__((__always_inline__)) type1 \ +__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \ + return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ +} + +#define __ssp_bos_icheck3(fun, type1, type2) \ +type1 __ ## fun ## _ichk(type1, type2, size_t); \ +inline __attribute__((__always_inline__)) type1 \ +__ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \ + return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ +} + +#define __ssp_bos_icheck2_restrict(fun, type1, type2) \ +type1 __ ## fun ## _ichk(type1, type2); \ +inline __attribute__((__always_inline__)) type1 \ +__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \ + return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \ +} + +__BEGIN_DECLS +__ssp_bos_icheck3_restrict(memcpy, void *, const void *) +__ssp_bos_icheck3(memmove, void *, const void *) +__ssp_bos_icheck3(memset, void *, int) +__ssp_bos_icheck2_restrict(strcpy, char *, const char *) +__ssp_bos_icheck2_restrict(strcat, char *, const char *) +__ssp_bos_icheck3_restrict(strncpy, char *, const char *) +__ssp_bos_icheck3_restrict(strncat, char *, const char *) +__END_DECLS + +#define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len) +#define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len) +#define memset(dst, val, len) __ssp_bos_check3(memset, dst, val, len) +#define bcopy(src, dst, len) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___memmove_chk(dst, src, __ssp_bos0(dst)) : \ + __memmove_ichk(dst, src, len)) +#define bzero(dst, len) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___memset_chk(dst, 0, __ssp_bos0(dst)) : \ + __memset_ichk(dst, src, len)) +#define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src) +#define strcat(dst, src) __ssp_bos_check2(strcat, dst, src) +#define strncpy(dst, src, lne) __ssp_bos_check3(strncpy, dst, src, len) +#define strncat(dst, src, lne) __ssp_bos_check3(strncat, dst, src, len) + +#endif /* __SSP_FORTIFY_LEVEL > 0 */ +#endif /* _SSP_STRING_H_ */ diff --git a/lib/libssp/ssp/unistd.h b/lib/libssp/ssp/unistd.h new file mode 100644 index 000000000000..1c0560faa2e8 --- /dev/null +++ b/lib/libssp/ssp/unistd.h @@ -0,0 +1,63 @@ +/* $NetBSD: unistd.h,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ +#ifndef _SSP_UNISTD_H_ +#define _SSP_UNISTD_H_ + +#include + +#include_next + +#if __SSP_FORTIFY_LEVEL > 0 +__BEGIN_DECLS + +__ssp_redirect0(ssize_t, read, (int __fd, void *__buf, size_t __len), \ + (__fd, __buf, __len)); + +__ssp_redirect(int, readlink, (const char *__restrict __path, \ + char *__restrict __buf, size_t __len), (__path, __buf, __len)); + +__ssp_redirect(char *, getcwd, (char *__buf, size_t __len), (__buf, __len)); + +__END_DECLS + +#define read(fd, buf, len) __read_alias(fd, buf, len) +#define readlink(path, buf, len) __readlink_alias(path, buf, len) +#define getcwd(buf, len) __getcwd_alias(buf, len) + +#endif /* __SSP_FORTIFY_LEVEL > 0 */ +#endif /* _SSP_UNISTD_H_ */ diff --git a/lib/libssp/strcat_chk.c b/lib/libssp/strcat_chk.c new file mode 100644 index 000000000000..5b68057678a3 --- /dev/null +++ b/lib/libssp/strcat_chk.c @@ -0,0 +1,65 @@ +/* $NetBSD: strcat_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: strcat_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +char * +__strcat_chk(char * __restrict dst, char * __restrict src, size_t slen) +{ + char *d; + + for (d = dst; *d; d++) { + if (slen-- == 0) + __chk_fail(); + } + + while (*src) { + if (slen-- == 0) + __chk_fail(); + *d++ = *src++; + } + + if (slen-- == 0) + __chk_fail(); + + *d = '\0'; + return dst; +} diff --git a/lib/libssp/strcpy_chk.c b/lib/libssp/strcpy_chk.c new file mode 100644 index 000000000000..b084b67150da --- /dev/null +++ b/lib/libssp/strcpy_chk.c @@ -0,0 +1,53 @@ +/* $NetBSD: strcpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: strcpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +char * +__strcpy_chk(char * __restrict dst, const char * __restrict src, size_t slen) +{ + size_t len = strlen(src); + + if (len > slen) + __chk_fail(); + + return memcpy(dst, src, len + 1); +} diff --git a/lib/libssp/strncat_chk.c b/lib/libssp/strncat_chk.c new file mode 100644 index 000000000000..cd260233f906 --- /dev/null +++ b/lib/libssp/strncat_chk.c @@ -0,0 +1,75 @@ +/* $NetBSD: strncat_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: strncat_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include + +char * +__strncat_chk(char * __restrict dst, char * __restrict src, size_t len, + size_t slen) +{ + char *d; + + if (len == 0) + return dst; + + if (len > slen) + __chk_fail(); + + for (d = dst; *d; d++) { + if (slen-- == 0) + __chk_fail(); + } + + do { + if ((*d = *src++) == '\0') + break; + if (slen-- == 0) + __chk_fail(); + d++; + } while (--len != 0); + + if (slen-- == 0) + __chk_fail(); + + *d = '\0'; + return dst; +} diff --git a/lib/libssp/strncpy_chk.c b/lib/libssp/strncpy_chk.c new file mode 100644 index 000000000000..16dfff972937 --- /dev/null +++ b/lib/libssp/strncpy_chk.c @@ -0,0 +1,52 @@ +/* $NetBSD: strncpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: strncpy_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include + +char * +__strncpy_chk(char * __restrict dst, const char * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + + return strncpy(dst, src, len); +} diff --git a/lib/libssp/vsnprintf_chk.c b/lib/libssp/vsnprintf_chk.c new file mode 100644 index 000000000000..32c74e3ac4d1 --- /dev/null +++ b/lib/libssp/vsnprintf_chk.c @@ -0,0 +1,53 @@ +/* $NetBSD: vsnprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: vsnprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include + +int +__vsnprintf_chk(char * __restrict buf, size_t len, int flags, size_t slen, + const char * __restrict fmt, va_list ap) +{ + if (len > slen) + __chk_fail(); + + return vsnprintf(buf, len, fmt, ap); +} diff --git a/lib/libssp/vsprintf_chk.c b/lib/libssp/vsprintf_chk.c new file mode 100644 index 000000000000..a6adff87b75d --- /dev/null +++ b/lib/libssp/vsprintf_chk.c @@ -0,0 +1,61 @@ +/* $NetBSD: vsprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 +__RCSID("$NetBSD: vsprintf_chk.c,v 1.1 2006/11/08 19:52:11 christos Exp $"); + +#include +#include +#include +#include + +/*ARGSUSED*/ +int +__vsprintf_chk(char * __restrict buf, int flags, size_t slen, + const char * __restrict fmt, va_list ap) +{ + int rv; + + if (slen > (size_t)INT_MAX) + rv = vsprintf(buf, fmt, ap); + else { + if ((rv = vsnprintf(buf, slen, fmt, ap)) >= 0 && rv >= slen) + __chk_fail(); + } + + return rv; +}