improve out-of-bounds checking with GCC 10 attribute access [BZ #25219]
[platform/upstream/glibc.git] / posix / bits / unistd.h
1 /* Checking macros for unistd functions.
2    Copyright (C) 2005-2020 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #ifndef _UNISTD_H
20 # error "Never include <bits/unistd.h> directly; use <unistd.h> instead."
21 #endif
22
23 extern ssize_t __read_chk (int __fd, void *__buf, size_t __nbytes,
24                            size_t __buflen)
25   __wur __attr_access ((__write_only__, 2, 3));
26 extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
27                                           size_t __nbytes), read)
28   __wur __attr_access ((__write_only__, 2, 3));
29 extern ssize_t __REDIRECT (__read_chk_warn,
30                            (int __fd, void *__buf, size_t __nbytes,
31                             size_t __buflen), __read_chk)
32      __wur __warnattr ("read called with bigger length than size of "
33                        "the destination buffer");
34
35 __fortify_function __wur ssize_t
36 read (int __fd, void *__buf, size_t __nbytes)
37 {
38   if (__bos0 (__buf) != (size_t) -1)
39     {
40       if (!__builtin_constant_p (__nbytes))
41         return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
42
43       if (__nbytes > __bos0 (__buf))
44         return __read_chk_warn (__fd, __buf, __nbytes, __bos0 (__buf));
45     }
46   return __read_alias (__fd, __buf, __nbytes);
47 }
48
49 #ifdef __USE_UNIX98
50 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
51                             __off_t __offset, size_t __bufsize)
52   __wur __attr_access ((__write_only__, 2, 3));
53 extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
54                               __off64_t __offset, size_t __bufsize)
55   __wur __attr_access ((__write_only__, 2, 3));
56 extern ssize_t __REDIRECT (__pread_alias,
57                            (int __fd, void *__buf, size_t __nbytes,
58                             __off_t __offset), pread)
59   __wur __attr_access ((__write_only__, 2, 3));
60 extern ssize_t __REDIRECT (__pread64_alias,
61                            (int __fd, void *__buf, size_t __nbytes,
62                             __off64_t __offset), pread64)
63   __wur __attr_access ((__write_only__, 2, 3));
64 extern ssize_t __REDIRECT (__pread_chk_warn,
65                            (int __fd, void *__buf, size_t __nbytes,
66                             __off_t __offset, size_t __bufsize), __pread_chk)
67      __wur __warnattr ("pread called with bigger length than size of "
68                        "the destination buffer");
69 extern ssize_t __REDIRECT (__pread64_chk_warn,
70                            (int __fd, void *__buf, size_t __nbytes,
71                             __off64_t __offset, size_t __bufsize),
72                             __pread64_chk)
73      __wur __warnattr ("pread64 called with bigger length than size of "
74                        "the destination buffer");
75
76 # ifndef __USE_FILE_OFFSET64
77 __fortify_function __wur ssize_t
78 pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
79 {
80   if (__bos0 (__buf) != (size_t) -1)
81     {
82       if (!__builtin_constant_p (__nbytes))
83         return __pread_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
84
85       if ( __nbytes > __bos0 (__buf))
86         return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
87                                  __bos0 (__buf));
88     }
89   return __pread_alias (__fd, __buf, __nbytes, __offset);
90 }
91 # else
92 __fortify_function __wur ssize_t
93 pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
94 {
95   if (__bos0 (__buf) != (size_t) -1)
96     {
97       if (!__builtin_constant_p (__nbytes))
98         return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
99
100       if ( __nbytes > __bos0 (__buf))
101         return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
102                                    __bos0 (__buf));
103     }
104
105   return __pread64_alias (__fd, __buf, __nbytes, __offset);
106 }
107 # endif
108
109 # ifdef __USE_LARGEFILE64
110 __fortify_function __wur ssize_t
111 pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
112 {
113   if (__bos0 (__buf) != (size_t) -1)
114     {
115       if (!__builtin_constant_p (__nbytes))
116         return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
117
118       if ( __nbytes > __bos0 (__buf))
119         return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
120                                    __bos0 (__buf));
121     }
122
123   return __pread64_alias (__fd, __buf, __nbytes, __offset);
124 }
125 # endif
126 #endif
127
128 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
129 extern ssize_t __readlink_chk (const char *__restrict __path,
130                                char *__restrict __buf, size_t __len,
131                                size_t __buflen)
132      __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
133 extern ssize_t __REDIRECT_NTH (__readlink_alias,
134                                (const char *__restrict __path,
135                                 char *__restrict __buf, size_t __len), readlink)
136      __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
137 extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
138                                (const char *__restrict __path,
139                                 char *__restrict __buf, size_t __len,
140                                 size_t __buflen), __readlink_chk)
141      __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
142                                           "than size of destination buffer");
143
144 __fortify_function __nonnull ((1, 2)) __wur ssize_t
145 __NTH (readlink (const char *__restrict __path, char *__restrict __buf,
146                  size_t __len))
147 {
148   if (__bos (__buf) != (size_t) -1)
149     {
150       if (!__builtin_constant_p (__len))
151         return __readlink_chk (__path, __buf, __len, __bos (__buf));
152
153       if ( __len > __bos (__buf))
154         return __readlink_chk_warn (__path, __buf, __len, __bos (__buf));
155     }
156   return __readlink_alias (__path, __buf, __len);
157 }
158 #endif
159
160 #ifdef __USE_ATFILE
161 extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
162                                  char *__restrict __buf, size_t __len,
163                                  size_t __buflen)
164      __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
165 extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
166                                (int __fd, const char *__restrict __path,
167                                 char *__restrict __buf, size_t __len),
168                                readlinkat)
169      __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
170 extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
171                                (int __fd, const char *__restrict __path,
172                                 char *__restrict __buf, size_t __len,
173                                 size_t __buflen), __readlinkat_chk)
174      __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
175                                           "length than size of destination "
176                                           "buffer");
177
178 __fortify_function __nonnull ((2, 3)) __wur ssize_t
179 __NTH (readlinkat (int __fd, const char *__restrict __path,
180                    char *__restrict __buf, size_t __len))
181 {
182   if (__bos (__buf) != (size_t) -1)
183     {
184       if (!__builtin_constant_p (__len))
185         return __readlinkat_chk (__fd, __path, __buf, __len, __bos (__buf));
186
187       if (__len > __bos (__buf))
188         return __readlinkat_chk_warn (__fd, __path, __buf, __len,
189                                       __bos (__buf));
190     }
191   return __readlinkat_alias (__fd, __path, __buf, __len);
192 }
193 #endif
194
195 extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
196      __THROW __wur __attr_access ((__write_only__, 1, 2));
197 extern char *__REDIRECT_NTH (__getcwd_alias,
198                              (char *__buf, size_t __size), getcwd)
199   __wur __attr_access ((__write_only__, 1, 2));
200 extern char *__REDIRECT_NTH (__getcwd_chk_warn,
201                              (char *__buf, size_t __size, size_t __buflen),
202                              __getcwd_chk)
203      __wur __warnattr ("getcwd caller with bigger length than size of "
204                        "destination buffer");
205
206 __fortify_function __wur char *
207 __NTH (getcwd (char *__buf, size_t __size))
208 {
209   if (__bos (__buf) != (size_t) -1)
210     {
211       if (!__builtin_constant_p (__size))
212         return __getcwd_chk (__buf, __size, __bos (__buf));
213
214       if (__size > __bos (__buf))
215         return __getcwd_chk_warn (__buf, __size, __bos (__buf));
216     }
217   return __getcwd_alias (__buf, __size);
218 }
219
220 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
221 extern char *__getwd_chk (char *__buf, size_t buflen)
222      __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
223 extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
224      __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
225                                        "doesn't specify buffer size");
226
227 __fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
228 __NTH (getwd (char *__buf))
229 {
230   if (__bos (__buf) != (size_t) -1)
231     return __getwd_chk (__buf, __bos (__buf));
232   return __getwd_warn (__buf);
233 }
234 #endif
235
236 extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
237                              size_t __buflen) __THROW
238   __attr_access ((__write_only__, 2, 3));
239 extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
240                                                 size_t __len), confstr)
241    __attr_access ((__write_only__, 2, 3));
242 extern size_t __REDIRECT_NTH (__confstr_chk_warn,
243                               (int __name, char *__buf, size_t __len,
244                                size_t __buflen), __confstr_chk)
245      __warnattr ("confstr called with bigger length than size of destination "
246                  "buffer");
247
248 __fortify_function size_t
249 __NTH (confstr (int __name, char *__buf, size_t __len))
250 {
251   if (__bos (__buf) != (size_t) -1)
252     {
253       if (!__builtin_constant_p (__len))
254         return __confstr_chk (__name, __buf, __len, __bos (__buf));
255
256       if (__bos (__buf) < __len)
257         return __confstr_chk_warn (__name, __buf, __len, __bos (__buf));
258     }
259   return __confstr_alias (__name, __buf, __len);
260 }
261
262
263 extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
264   __THROW __wur __attr_access ((__write_only__, 2, 1));
265 extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
266                            getgroups) __wur __attr_access ((__write_only__, 2, 1));
267 extern int __REDIRECT_NTH (__getgroups_chk_warn,
268                            (int __size, __gid_t __list[], size_t __listlen),
269                            __getgroups_chk)
270      __wur __warnattr ("getgroups called with bigger group count than what "
271                        "can fit into destination buffer");
272
273 __fortify_function int
274 __NTH (getgroups (int __size, __gid_t __list[]))
275 {
276   if (__bos (__list) != (size_t) -1)
277     {
278       if (!__builtin_constant_p (__size) || __size < 0)
279         return __getgroups_chk (__size, __list, __bos (__list));
280
281       if (__size * sizeof (__gid_t) > __bos (__list))
282         return __getgroups_chk_warn (__size, __list, __bos (__list));
283     }
284   return __getgroups_alias (__size, __list);
285 }
286
287
288 extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
289                             size_t __nreal) __THROW __nonnull ((2))
290    __attr_access ((__write_only__, 2, 3));
291 extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
292                                                size_t __buflen), ttyname_r)
293      __nonnull ((2));
294 extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
295                            (int __fd, char *__buf, size_t __buflen,
296                             size_t __nreal), __ttyname_r_chk)
297      __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
298                                  "size of destination buffer");
299
300 __fortify_function int
301 __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
302 {
303   if (__bos (__buf) != (size_t) -1)
304     {
305       if (!__builtin_constant_p (__buflen))
306         return __ttyname_r_chk (__fd, __buf, __buflen, __bos (__buf));
307
308       if (__buflen > __bos (__buf))
309         return __ttyname_r_chk_warn (__fd, __buf, __buflen, __bos (__buf));
310     }
311   return __ttyname_r_alias (__fd, __buf, __buflen);
312 }
313
314
315 #ifdef __USE_POSIX199506
316 extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
317      __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
318 extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
319                        getlogin_r) __nonnull ((1));
320 extern int __REDIRECT (__getlogin_r_chk_warn,
321                        (char *__buf, size_t __buflen, size_t __nreal),
322                        __getlogin_r_chk)
323      __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
324                                  "size of destination buffer");
325
326 __fortify_function int
327 getlogin_r (char *__buf, size_t __buflen)
328 {
329   if (__bos (__buf) != (size_t) -1)
330     {
331       if (!__builtin_constant_p (__buflen))
332         return __getlogin_r_chk (__buf, __buflen, __bos (__buf));
333
334       if (__buflen > __bos (__buf))
335         return __getlogin_r_chk_warn (__buf, __buflen, __bos (__buf));
336     }
337   return __getlogin_r_alias (__buf, __buflen);
338 }
339 #endif
340
341
342 #if defined __USE_MISC || defined __USE_UNIX98
343 extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
344      __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
345 extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
346                            gethostname)
347   __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
348 extern int __REDIRECT_NTH (__gethostname_chk_warn,
349                            (char *__buf, size_t __buflen, size_t __nreal),
350                            __gethostname_chk)
351      __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
352                                  "size of destination buffer");
353
354 __fortify_function int
355 __NTH (gethostname (char *__buf, size_t __buflen))
356 {
357   if (__bos (__buf) != (size_t) -1)
358     {
359       if (!__builtin_constant_p (__buflen))
360         return __gethostname_chk (__buf, __buflen, __bos (__buf));
361
362       if (__buflen > __bos (__buf))
363         return __gethostname_chk_warn (__buf, __buflen, __bos (__buf));
364     }
365   return __gethostname_alias (__buf, __buflen);
366 }
367 #endif
368
369
370 #if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
371 extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
372      __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
373 extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
374                                                    size_t __buflen),
375                            getdomainname) __nonnull ((1))
376   __wur __attr_access ((__write_only__, 1, 2));
377 extern int __REDIRECT_NTH (__getdomainname_chk_warn,
378                            (char *__buf, size_t __buflen, size_t __nreal),
379                            __getdomainname_chk)
380      __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
381                                        "buflen than size of destination "
382                                        "buffer");
383
384 __fortify_function int
385 __NTH (getdomainname (char *__buf, size_t __buflen))
386 {
387   if (__bos (__buf) != (size_t) -1)
388     {
389       if (!__builtin_constant_p (__buflen))
390         return __getdomainname_chk (__buf, __buflen, __bos (__buf));
391
392       if (__buflen > __bos (__buf))
393         return __getdomainname_chk_warn (__buf, __buflen, __bos (__buf));
394     }
395   return __getdomainname_alias (__buf, __buflen);
396 }
397 #endif