Ok, I've tried being Mr. Nice Guy and people (you know who you are) still
[tprouty/samba.git] / source / include / safe_string.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Safe string handling routines.
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifndef _SAFE_STRING_H
23 #define _SAFE_STRING_H
24
25 #ifndef _SPLINT_ /* http://www.splint.org */
26
27 /* Some macros to ensure people don't use buffer overflow vulnerable string
28    functions. */
29
30 #ifdef bcopy
31 #undef bcopy
32 #endif /* bcopy */
33 #define bcopy(src,dest,size) __ERROR__XX__NEVER_USE_BCOPY___;
34
35 #ifdef bzero
36 #undef bzero
37 #endif /* bzero */
38 #define bzero(dest,size) __ERROR__XX__NEVER_USE_BZERO_USE_MEMSET_INSTEAD___;
39
40 #ifdef strcpy
41 #undef strcpy
42 #endif /* strcpy */
43 #define strcpy(dest,src) __ERROR__XX__NEVER_USE_STRCPY___;
44
45 #ifdef strcat
46 #undef strcat
47 #endif /* strcat */
48 #define strcat(dest,src) __ERROR__XX__NEVER_USE_STRCAT___;
49
50 #ifdef sprintf
51 #undef sprintf
52 #endif /* sprintf */
53 #define sprintf __ERROR__XX__NEVER_USE_SPRINTF__;
54
55 #endif /* !_SPLINT_ */
56
57 #ifdef DEVELOPER
58 #define SAFE_STRING_FUNCTION_NAME FUNCTION_MACRO
59 #define SAFE_STRING_LINE __LINE__
60 #else
61 #define SAFE_STRING_FUNCTION_NAME ("")
62 #define SAFE_STRING_LINE (0)
63 #endif
64
65 /* We need a number of different prototypes for our 
66    non-existant fuctions */
67 char * __unsafe_string_function_usage_here__(void);
68
69 size_t __unsafe_string_function_usage_here_size_t__(void);
70
71 size_t __unsafe_string_function_usage_here_char__(void);
72
73 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
74
75 /* if the compiler will optimize out function calls, then use this to tell if we are 
76    have the correct types (this works only where sizeof() returns the size of the buffer, not
77    the size of the pointer). */
78
79 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
80
81 #define fstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(fstring)) \
82     ? __unsafe_string_function_usage_here_char__() \
83     : (((d)[sizeof(fstring)-1]) = '\0'))
84 #define pstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(pstring)) \
85     ? __unsafe_string_function_usage_here_char__() \
86     : (((d)[sizeof(pstring)-1]) = '\0'))
87
88 #define wpstrcpy(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
89     ? __unsafe_string_function_usage_here__() \
90     : safe_strcpy_w((d),(s),sizeof(wpstring)))
91 #define wpstrcat(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
92     ? __unsafe_string_function_usage_here__() \
93     : safe_strcat_w((d),(s),sizeof(wpstring)))
94 #define wfstrcpy(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
95     ? __unsafe_string_function_usage_here__() \
96     : safe_strcpy_w((d),(s),sizeof(wfstring)))
97 #define wfstrcat(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
98     ? __unsafe_string_function_usage_here__() \
99     : safe_strcat_w((d),(s),sizeof(wfstring)))
100
101 #define push_pstring_base(dest, src, pstring_base) \
102     (CHECK_STRING_SIZE(pstring_base, sizeof(pstring)) \
103     ? __unsafe_string_function_usage_here_size_t__() \
104     : push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE))
105
106 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
107
108 #define fstrterminate(d) (((d)[sizeof(fstring)-1]) = '\0')
109 #define pstrterminate(d) (((d)[sizeof(pstring)-1]) = '\0')
110
111 #define wpstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wpstring))
112 #define wpstrcat(d,s) safe_strcat_w((d),(s),sizeof(wpstring))
113 #define wfstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wfstring))
114 #define wfstrcat(d,s) safe_strcat_w((d),(s),sizeof(wfstring))
115
116 #define push_pstring_base(dest, src, pstring_base) \
117     push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE)
118
119 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
120
121 #define safe_strcpy_base(dest, src, base, size) \
122     safe_strcpy(dest, src, size-PTR_DIFF(dest,base)-1)
123
124 /* String copy functions - macro hell below adds 'type checking' (limited,
125    but the best we can do in C) and may tag with function name/number to
126    record the last 'clobber region' on that string */
127
128 #define pstrcpy(d,s) safe_strcpy((d), (s),sizeof(pstring)-1)
129 #define pstrcat(d,s) safe_strcat((d), (s),sizeof(pstring)-1)
130 #define fstrcpy(d,s) safe_strcpy((d),(s),sizeof(fstring)-1)
131 #define fstrcat(d,s) safe_strcat((d),(s),sizeof(fstring)-1)
132
133 /* the addition of the DEVELOPER checks in safe_strcpy means we must
134  * update a lot of code. To make this a little easier here are some
135  * functions that provide the lengths with less pain */
136 #define pstrcpy_base(dest, src, pstring_base) \
137     safe_strcpy(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1)
138
139
140 /* Inside the _fn variants of these is a call to clobber_region(), -
141  * which might destroy the stack on a buggy function.  We help the
142  * debugging process by putting the function and line who last caused
143  * a clobbering into a static buffer.  If the program crashes at
144  * address 0xf1f1f1f1 then this function is probably, but not
145  * necessarily, to blame. */
146
147 /* overmalloc_safe_strcpy: DEPRECATED!  Used when you know the
148  * destination buffer is longer than maxlength, but you don't know how
149  * long.  This is not a good situation, because we can't do the normal
150  * sanity checks. Don't use in new code! */
151
152 #define overmalloc_safe_strcpy(dest,src,maxlength)      safe_strcpy_fn(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
153 #define safe_strcpy(dest,src,maxlength) safe_strcpy_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
154 #define safe_strcat(dest,src,maxlength) safe_strcat_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
155 #define push_string(base_ptr, dest, src, dest_len, flags) push_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
156 #define pull_string(base_ptr, dest, src, dest_len, src_len, flags) pull_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, src_len, flags)
157 #define clistr_push(cli, dest, src, dest_len, flags) clistr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, flags)
158 #define clistr_pull(cli, dest, src, dest_len, src_len, flags) clistr_pull_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, src_len, flags)
159 #define srvstr_push(base_ptr, dest, src, dest_len, flags) srvstr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
160
161 #define alpha_strcpy(dest,src,other_safe_chars,maxlength) alpha_strcpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,other_safe_chars,maxlength)
162 #define StrnCpy(dest,src,n)             StrnCpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,n)
163
164 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
165
166 /* if the compiler will optimize out function calls, then use this to tell if we are 
167    have the correct types (this works only where sizeof() returns the size of the buffer, not
168    the size of the pointer). */
169
170 #define safe_strcpy_fn2(fn_name, fn_line, d, s, max_len) \
171     (CHECK_STRING_SIZE(d, max_len+1) \
172     ? __unsafe_string_function_usage_here__() \
173     : safe_strcpy_fn(fn_name, fn_line, (d), (s), (max_len)))
174
175 #define safe_strcat_fn2(fn_name, fn_line, d, s, max_len) \
176     (CHECK_STRING_SIZE(d, max_len+1) \
177     ? __unsafe_string_function_usage_here__() \
178     : safe_strcat_fn(fn_name, fn_line, (d), (s), (max_len)))
179
180 #define push_string_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
181     (CHECK_STRING_SIZE(dest, dest_len) \
182     ? __unsafe_string_function_usage_here_size_t__() \
183     : push_string_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
184
185 #define pull_string_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, src_len, flags) \
186     (CHECK_STRING_SIZE(dest, dest_len) \
187     ? __unsafe_string_function_usage_here_size_t__() \
188     : pull_string_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, src_len, flags))
189
190 #define clistr_push_fn2(fn_name, fn_line, cli, dest, src, dest_len, flags) \
191     (CHECK_STRING_SIZE(dest, dest_len) \
192     ? __unsafe_string_function_usage_here_size_t__() \
193     : clistr_push_fn(fn_name, fn_line, cli, dest, src, dest_len, flags))
194
195 #define clistr_pull_fn2(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags) \
196     (CHECK_STRING_SIZE(dest, dest_len) \
197     ? __unsafe_string_function_usage_here_size_t__() \
198     : clistr_pull_fn(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags))
199
200 #define srvstr_push_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
201     (CHECK_STRING_SIZE(dest, dest_len) \
202     ? __unsafe_string_function_usage_here_size_t__() \
203     : srvstr_push_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
204
205 #else
206
207 #define safe_strcpy_fn2 safe_strcpy_fn
208 #define safe_strcat_fn2 safe_strcat_fn
209 #define push_string_fn2 push_string_fn
210 #define pull_string_fn2 pull_string_fn
211 #define clistr_push_fn2 clistr_push_fn
212 #define clistr_pull_fn2 clistr_pull_fn
213 #define srvstr_push_fn2 srvstr_push_fn
214
215 #endif
216
217 /* replace some string functions with multi-byte
218    versions */
219 #define strlower(s) strlower_m(s)
220 #define strupper(s) strupper_m(s)
221
222 #endif