1be04fec4SKyle Evans /* $NetBSD: string.h,v 1.14 2020/09/05 13:37:59 mrg Exp $ */ 2be04fec4SKyle Evans 3be04fec4SKyle Evans /*- 4be04fec4SKyle Evans * 5be04fec4SKyle Evans * SPDX-License-Identifier: BSD-2-Clause 6be04fec4SKyle Evans * 7be04fec4SKyle Evans * Copyright (c) 2006 The NetBSD Foundation, Inc. 8be04fec4SKyle Evans * All rights reserved. 9be04fec4SKyle Evans * 10be04fec4SKyle Evans * This code is derived from software contributed to The NetBSD Foundation 11be04fec4SKyle Evans * by Christos Zoulas. 12be04fec4SKyle Evans * 13be04fec4SKyle Evans * Redistribution and use in source and binary forms, with or without 14be04fec4SKyle Evans * modification, are permitted provided that the following conditions 15be04fec4SKyle Evans * are met: 16be04fec4SKyle Evans * 1. Redistributions of source code must retain the above copyright 17be04fec4SKyle Evans * notice, this list of conditions and the following disclaimer. 18be04fec4SKyle Evans * 2. Redistributions in binary form must reproduce the above copyright 19be04fec4SKyle Evans * notice, this list of conditions and the following disclaimer in the 20be04fec4SKyle Evans * documentation and/or other materials provided with the distribution. 21be04fec4SKyle Evans * 22be04fec4SKyle Evans * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23be04fec4SKyle Evans * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24be04fec4SKyle Evans * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25be04fec4SKyle Evans * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26be04fec4SKyle Evans * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27be04fec4SKyle Evans * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28be04fec4SKyle Evans * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29be04fec4SKyle Evans * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30be04fec4SKyle Evans * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31be04fec4SKyle Evans * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32be04fec4SKyle Evans * POSSIBILITY OF SUCH DAMAGE. 33be04fec4SKyle Evans */ 34be04fec4SKyle Evans #ifndef _SSP_STRING_H_ 35be04fec4SKyle Evans #define _SSP_STRING_H_ 36be04fec4SKyle Evans 37be04fec4SKyle Evans #include <ssp/ssp.h> 38be04fec4SKyle Evans 39be04fec4SKyle Evans __BEGIN_DECLS 40be04fec4SKyle Evans void *__memcpy_chk(void *, const void *, size_t, size_t); 41be04fec4SKyle Evans void *__memmove_chk(void *, const void *, size_t, size_t); 42be04fec4SKyle Evans void *__memset_chk(void *, int, size_t, size_t); 43be04fec4SKyle Evans char *__stpcpy_chk(char *, const char *, size_t); 44be04fec4SKyle Evans char *__stpncpy_chk(char *, const char *, size_t, size_t); 45be04fec4SKyle Evans char *__strcat_chk(char *, const char *, size_t); 46be04fec4SKyle Evans char *__strcpy_chk(char *, const char *, size_t); 47be04fec4SKyle Evans char *__strncat_chk(char *, const char *, size_t, size_t); 48cf8e5289SKyle Evans size_t __strlcat_chk(char *, const char *, size_t, size_t); 49be04fec4SKyle Evans char *__strncpy_chk(char *, const char *, size_t, size_t); 50cf8e5289SKyle Evans size_t __strlcpy_chk(char *, const char *, size_t, size_t); 51be04fec4SKyle Evans __END_DECLS 52be04fec4SKyle Evans 53be04fec4SKyle Evans #if __SSP_FORTIFY_LEVEL > 0 54be04fec4SKyle Evans 55be04fec4SKyle Evans #define __ssp_bos_check3_typed_var(fun, dsttype, dsrvar, dst, srctype, srcvar, \ 565af6fbd7SKyle Evans src, lenvar, len) __extension__ ({ \ 57be04fec4SKyle Evans srctype srcvar = (src); \ 58be04fec4SKyle Evans dsttype dstvar = (dst); \ 59be04fec4SKyle Evans size_t lenvar = (len); \ 60be04fec4SKyle Evans ((__ssp_bos0(dstvar) != (size_t)-1) ? \ 61be04fec4SKyle Evans __builtin___ ## fun ## _chk(dstvar, srcvar, lenvar, \ 62be04fec4SKyle Evans __ssp_bos0(dstvar)) : \ 63be04fec4SKyle Evans __ ## fun ## _ichk(dstvar, srcvar, lenvar)); \ 64be04fec4SKyle Evans }) 65be04fec4SKyle Evans 66be04fec4SKyle Evans #define __ssp_bos_check3_typed(fun, dsttype, dst, srctype, src, len) \ 67be04fec4SKyle Evans __ssp_bos_check3_typed_var(fun, dsttype, __ssp_var(dstv), dst, \ 68be04fec4SKyle Evans srctype, __ssp_var(srcv), src, __ssp_var(lenv), len) 69be04fec4SKyle Evans 70be04fec4SKyle Evans #define __ssp_bos_check3(fun, dst, src, len) \ 71be04fec4SKyle Evans __ssp_bos_check3_typed_var(fun, void *, __ssp_var(dstv), dst, \ 72be04fec4SKyle Evans const void *, __ssp_var(srcv), src, __ssp_var(lenv), len) 73be04fec4SKyle Evans 745af6fbd7SKyle Evans #define __ssp_bos_check2_var(fun, dstvar, dst, srcvar, src) __extension__ ({ \ 75be04fec4SKyle Evans const void *srcvar = (src); \ 76be04fec4SKyle Evans void *dstvar = (dst); \ 77be04fec4SKyle Evans ((__ssp_bos0(dstvar) != (size_t)-1) ? \ 78be04fec4SKyle Evans __builtin___ ## fun ## _chk(dstvar, srcvar, \ 79be04fec4SKyle Evans __ssp_bos0(dstvar)) : \ 80be04fec4SKyle Evans __ ## fun ## _ichk(dstvar, srcvar)); \ 81be04fec4SKyle Evans }) 82be04fec4SKyle Evans 83be04fec4SKyle Evans #define __ssp_bos_check2(fun, dst, src) \ 84be04fec4SKyle Evans __ssp_bos_check2_var(fun, __ssp_var(dstv), dst, __ssp_var(srcv), src) 85be04fec4SKyle Evans 86be04fec4SKyle Evans #define __ssp_bos_icheck3_restrict(fun, type1, type2) \ 87be04fec4SKyle Evans static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \ 88be04fec4SKyle Evans static __inline __attribute__((__always_inline__)) type1 \ 89be04fec4SKyle Evans __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \ 90be04fec4SKyle Evans return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ 91be04fec4SKyle Evans } 92be04fec4SKyle Evans 93be04fec4SKyle Evans #define __ssp_bos_icheck3(fun, type1, type2) \ 94be04fec4SKyle Evans static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \ 95be04fec4SKyle Evans static __inline __attribute__((__always_inline__)) type1 \ 96be04fec4SKyle Evans __ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \ 97be04fec4SKyle Evans return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ 98be04fec4SKyle Evans } 99be04fec4SKyle Evans 100be04fec4SKyle Evans #define __ssp_bos_icheck2_restrict(fun, type1, type2) \ 101be04fec4SKyle Evans static __inline type1 __ ## fun ## _ichk(type1, type2); \ 102be04fec4SKyle Evans static __inline __attribute__((__always_inline__)) type1 \ 103be04fec4SKyle Evans __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \ 104be04fec4SKyle Evans return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \ 105be04fec4SKyle Evans } 106be04fec4SKyle Evans 107be04fec4SKyle Evans __BEGIN_DECLS 108be04fec4SKyle Evans __ssp_bos_icheck3_restrict(memcpy, void *, const void *) 1090c47b9c2SKyle Evans __ssp_bos_icheck3_restrict(mempcpy, void *, const void *) 110be04fec4SKyle Evans __ssp_bos_icheck3(memmove, void *, const void *) 111be04fec4SKyle Evans __ssp_bos_icheck3(memset, void *, int) 112*8983acc8SRobert Clausecker __ssp_redirect(void *, memset_explicit, (void *__buf, int __ch, size_t __len), 113*8983acc8SRobert Clausecker (__buf, __ch, __len)); 114be04fec4SKyle Evans __ssp_bos_icheck2_restrict(stpcpy, char *, const char *) 115be04fec4SKyle Evans __ssp_bos_icheck3_restrict(stpncpy, char *, const char *) 116be04fec4SKyle Evans __ssp_bos_icheck2_restrict(strcpy, char *, const char *) 117be04fec4SKyle Evans __ssp_bos_icheck2_restrict(strcat, char *, const char *) 118cf8e5289SKyle Evans __ssp_redirect0(int, strerror_r, (int __errnum, char *__buf, size_t __len), 119cf8e5289SKyle Evans (__errnum, __buf, __len)); 120be04fec4SKyle Evans __ssp_bos_icheck3_restrict(strncpy, char *, const char *) 121be04fec4SKyle Evans __ssp_bos_icheck3_restrict(strncat, char *, const char *) 122be04fec4SKyle Evans __END_DECLS 123be04fec4SKyle Evans 124be04fec4SKyle Evans #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len) 1250c47b9c2SKyle Evans #define mempcpy(dst, src, len) __ssp_bos_check3(mempcpy, dst, src, len) 126be04fec4SKyle Evans #define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len) 127be04fec4SKyle Evans #define memset(dst, val, len) \ 128be04fec4SKyle Evans __ssp_bos_check3_typed(memset, void *, dst, int, val, len) 129be04fec4SKyle Evans #define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src) 130be04fec4SKyle Evans #define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len) 131be04fec4SKyle Evans #define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src) 132be04fec4SKyle Evans #define strcat(dst, src) __ssp_bos_check2(strcat, dst, src) 133cf8e5289SKyle Evans #define strlcpy(dst, src, dstlen) \ 134cf8e5289SKyle Evans __strlcpy_chk(dst, src, dstlen, __ssp_bos(dst)) 135be04fec4SKyle Evans #define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len) 136cf8e5289SKyle Evans #define strlcat(dst, src, dstlen) \ 137cf8e5289SKyle Evans __strlcat_chk(dst, src, dstlen, __ssp_bos(dst)) 138be04fec4SKyle Evans #define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len) 139be04fec4SKyle Evans 140be04fec4SKyle Evans #endif /* __SSP_FORTIFY_LEVEL > 0 */ 141be04fec4SKyle Evans #endif /* _SSP_STRING_H_ */ 142