xref: /freebsd/include/ssp/stdio.h (revision 9137c66c2ea6cc09e3a6f8a042ecdc5a62e0f39e)
1 /*	$NetBSD: stdio.h,v 1.5 2011/07/17 20:54:34 joerg Exp $	*/
2 
3 /*-
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c) 2006 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Christos Zoulas.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef _SSP_STDIO_H_
35 #define _SSP_STDIO_H_
36 
37 #include <ssp/ssp.h>
38 
39 #if __SSP_FORTIFY_LEVEL > 0 && __EXT1_VISIBLE
40 #include <sys/stdint.h>
41 #endif
42 
43 __BEGIN_DECLS
44 #if __SSP_FORTIFY_LEVEL > 0
45 #if __POSIX_VISIBLE
46 __ssp_redirect_raw(char *, ctermid, ctermid, (char *__buf), (__buf),
47     __buf != NULL, __ssp_bos, L_ctermid);
48 #if __BSD_VISIBLE
49 __ssp_redirect_raw(char *, ctermid_r, ctermid_r, (char *__buf), (__buf),
50     __buf != NULL, __ssp_bos, L_ctermid);
51 #endif /* __BSD_VISIBLE */
52 #endif /* __POSIX_VISIBLE */
53 __ssp_redirect(size_t, fread, (void *__restrict __buf, size_t __len,
54     size_t __nmemb, FILE *__restrict __fp), (__buf, __len, __nmemb, __fp));
55 __ssp_redirect(size_t, fread_unlocked, (void *__restrict __buf, size_t __len,
56     size_t __nmemb, FILE *__restrict __fp), (__buf, __len, __nmemb, __fp));
57 #if __EXT1_VISIBLE
58 __ssp_redirect_raw_impl(char *, gets_s, gets_s,
59     (char *buf, rsize_t len))
60 {
61 	char *retbuf;
62 	size_t bufsz;
63 	int need_fail = 0;
64 
65 	/*
66 	 * If we would have overwritten our buffer, we want to fail the check
67 	 * only if these arguments wouldn't have triggered a constraint
68 	 * violation.
69 	 */
70 	bufsz = __ssp_bos(buf);
71 	if (bufsz != (size_t)-1 && (size_t)len > bufsz) {
72 		if (len <= RSIZE_MAX)
73 			__chk_fail();
74 		need_fail = 1;
75 	}
76 
77 	retbuf = __ssp_real(gets_s)(buf, len);
78 
79 	/*
80 	 * If the implementation did *not* handle the case correctly, then
81 	 * there's a risk that they could have corrupted us into not failing
82 	 * here.  We have tests that cover this, so we'll just count on finding
83 	 * a broken implementation early on in a less hostile environmnt.
84 	 */
85 	if (need_fail && retbuf != NULL)
86 		__chk_fail();
87 	return (retbuf);
88 }
89 
90 #endif /* __EXT1_VISIBLE */
91 __ssp_redirect_raw(char *, tmpnam, tmpnam, (char *__buf), (__buf), 1,
92     __ssp_bos, L_tmpnam);
93 #endif
94 
95 int __sprintf_chk(char *__restrict, int, size_t, const char *__restrict, ...)
96     __printflike(4, 5);
97 int __vsprintf_chk(char *__restrict, int, size_t, const char *__restrict,
98     __va_list)
99     __printflike(4, 0);
100 int __snprintf_chk(char *__restrict, size_t, int, size_t,
101     const char *__restrict, ...)
102     __printflike(5, 6);
103 int __vsnprintf_chk(char *__restrict, size_t, int, size_t,
104      const char *__restrict, __va_list)
105     __printflike(5, 0);
106 char *__fgets_chk(char *, int, size_t, FILE *);
107 __END_DECLS
108 
109 #if __SSP_FORTIFY_LEVEL > 0
110 
111 #define sprintf(str, ...) __extension__ ({	\
112     char *_ssp_str = (str);	\
113     __builtin___sprintf_chk(_ssp_str, 0, __ssp_bos(_ssp_str),		\
114         __VA_ARGS__); \
115 })
116 
117 #define vsprintf(str, fmt, ap) __extension__ ({	\
118     char *_ssp_str = (str);		\
119     __builtin___vsprintf_chk(_ssp_str, 0, __ssp_bos(_ssp_str), fmt,	\
120         ap);				\
121 })
122 
123 #define snprintf(str, len, ...) __extension__ ({	\
124     char *_ssp_str = (str);		\
125     __builtin___snprintf_chk(_ssp_str, len, 0, __ssp_bos(_ssp_str),	\
126         __VA_ARGS__);			\
127 })
128 
129 #define vsnprintf(str, len, fmt, ap) __extension__ ({	\
130     char *_ssp_str = (str);		\
131     __builtin___vsnprintf_chk(_ssp_str, len, 0, __ssp_bos(_ssp_str),	\
132         fmt, ap);			\
133 })
134 
135 #define fgets(str, len, fp) __extension__ ({		\
136     char *_ssp_str = (str);		\
137     __fgets_chk(_ssp_str, len, __ssp_bos(_ssp_str), fp);	\
138 })
139 
140 #endif /* __SSP_FORTIFY_LEVEL > 0 */
141 
142 #endif /* _SSP_STDIO_H_ */
143