1/* Checking macros for unistd functions.
2 Copyright (C) 2005-2021 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
23extern ssize_t __read_chk (int __fd, void *__buf, size_t __nbytes,
24 size_t __buflen)
25 __wur __attr_access ((__write_only__, 2, 3));
26extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
27 size_t __nbytes), read)
28 __wur __attr_access ((__write_only__, 2, 3));
29extern 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
36read (int __fd, void *__buf, size_t __nbytes)
37{
38 if (__glibc_objsize0 (__buf) != (size_t) -1)
39 {
40 if (!__builtin_constant_p (__nbytes))
41 return __read_chk (__fd, __buf, __nbytes, __glibc_objsize0 (__buf));
42
43 if (__nbytes > __glibc_objsize0 (__buf))
44 return __read_chk_warn (__fd, __buf, __nbytes,
45 __glibc_objsize0 (__buf));
46 }
47 return __read_alias (__fd, __buf, __nbytes);
48}
49
50#ifdef __USE_UNIX98
51extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
52 __off_t __offset, size_t __bufsize)
53 __wur __attr_access ((__write_only__, 2, 3));
54extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
55 __off64_t __offset, size_t __bufsize)
56 __wur __attr_access ((__write_only__, 2, 3));
57extern ssize_t __REDIRECT (__pread_alias,
58 (int __fd, void *__buf, size_t __nbytes,
59 __off_t __offset), pread)
60 __wur __attr_access ((__write_only__, 2, 3));
61extern ssize_t __REDIRECT (__pread64_alias,
62 (int __fd, void *__buf, size_t __nbytes,
63 __off64_t __offset), pread64)
64 __wur __attr_access ((__write_only__, 2, 3));
65extern ssize_t __REDIRECT (__pread_chk_warn,
66 (int __fd, void *__buf, size_t __nbytes,
67 __off_t __offset, size_t __bufsize), __pread_chk)
68 __wur __warnattr ("pread called with bigger length than size of "
69 "the destination buffer");
70extern ssize_t __REDIRECT (__pread64_chk_warn,
71 (int __fd, void *__buf, size_t __nbytes,
72 __off64_t __offset, size_t __bufsize),
73 __pread64_chk)
74 __wur __warnattr ("pread64 called with bigger length than size of "
75 "the destination buffer");
76
77# ifndef __USE_FILE_OFFSET64
78__fortify_function __wur ssize_t
79pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
80{
81 if (__glibc_objsize0 (__buf) != (size_t) -1)
82 {
83 if (!__builtin_constant_p (__nbytes))
84 return __pread_chk (__fd, __buf, __nbytes, __offset,
85 __glibc_objsize0 (__buf));
86
87 if ( __nbytes > __glibc_objsize0 (__buf))
88 return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
89 __glibc_objsize0 (__buf));
90 }
91 return __pread_alias (__fd, __buf, __nbytes, __offset);
92}
93# else
94__fortify_function __wur ssize_t
95pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
96{
97 if (__glibc_objsize0 (__buf) != (size_t) -1)
98 {
99 if (!__builtin_constant_p (__nbytes))
100 return __pread64_chk (__fd, __buf, __nbytes, __offset,
101 __glibc_objsize0 (__buf));
102
103 if ( __nbytes > __glibc_objsize0 (__buf))
104 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
105 __glibc_objsize0 (__buf));
106 }
107
108 return __pread64_alias (__fd, __buf, __nbytes, __offset);
109}
110# endif
111
112# ifdef __USE_LARGEFILE64
113__fortify_function __wur ssize_t
114pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
115{
116 if (__glibc_objsize0 (__buf) != (size_t) -1)
117 {
118 if (!__builtin_constant_p (__nbytes))
119 return __pread64_chk (__fd, __buf, __nbytes, __offset,
120 __glibc_objsize0 (__buf));
121
122 if ( __nbytes > __glibc_objsize0 (__buf))
123 return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
124 __glibc_objsize0 (__buf));
125 }
126
127 return __pread64_alias (__fd, __buf, __nbytes, __offset);
128}
129# endif
130#endif
131
132#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
133extern ssize_t __readlink_chk (const char *__restrict __path,
134 char *__restrict __buf, size_t __len,
135 size_t __buflen)
136 __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
137extern ssize_t __REDIRECT_NTH (__readlink_alias,
138 (const char *__restrict __path,
139 char *__restrict __buf, size_t __len), readlink)
140 __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
141extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
142 (const char *__restrict __path,
143 char *__restrict __buf, size_t __len,
144 size_t __buflen), __readlink_chk)
145 __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
146 "than size of destination buffer");
147
148__fortify_function __nonnull ((1, 2)) __wur ssize_t
149__NTH (readlink (const char *__restrict __path, char *__restrict __buf,
150 size_t __len))
151{
152 if (__glibc_objsize (__buf) != (size_t) -1)
153 {
154 if (!__builtin_constant_p (__len))
155 return __readlink_chk (__path, __buf, __len, __glibc_objsize (__buf));
156
157 if ( __len > __glibc_objsize (__buf))
158 return __readlink_chk_warn (__path, __buf, __len,
159 __glibc_objsize (__buf));
160 }
161 return __readlink_alias (__path, __buf, __len);
162}
163#endif
164
165#ifdef __USE_ATFILE
166extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
167 char *__restrict __buf, size_t __len,
168 size_t __buflen)
169 __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
170extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
171 (int __fd, const char *__restrict __path,
172 char *__restrict __buf, size_t __len),
173 readlinkat)
174 __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
175extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
176 (int __fd, const char *__restrict __path,
177 char *__restrict __buf, size_t __len,
178 size_t __buflen), __readlinkat_chk)
179 __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
180 "length than size of destination "
181 "buffer");
182
183__fortify_function __nonnull ((2, 3)) __wur ssize_t
184__NTH (readlinkat (int __fd, const char *__restrict __path,
185 char *__restrict __buf, size_t __len))
186{
187 if (__glibc_objsize (__buf) != (size_t) -1)
188 {
189 if (!__builtin_constant_p (__len))
190 return __readlinkat_chk (__fd, __path, __buf, __len,
191 __glibc_objsize (__buf));
192
193 if (__len > __glibc_objsize (__buf))
194 return __readlinkat_chk_warn (__fd, __path, __buf, __len,
195 __glibc_objsize (__buf));
196 }
197 return __readlinkat_alias (__fd, __path, __buf, __len);
198}
199#endif
200
201extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
202 __THROW __wur __attr_access ((__write_only__, 1, 2));
203extern char *__REDIRECT_NTH (__getcwd_alias,
204 (char *__buf, size_t __size), getcwd)
205 __wur __attr_access ((__write_only__, 1, 2));
206extern char *__REDIRECT_NTH (__getcwd_chk_warn,
207 (char *__buf, size_t __size, size_t __buflen),
208 __getcwd_chk)
209 __wur __warnattr ("getcwd caller with bigger length than size of "
210 "destination buffer");
211
212__fortify_function __wur char *
213__NTH (getcwd (char *__buf, size_t __size))
214{
215 if (__glibc_objsize (__buf) != (size_t) -1)
216 {
217 if (!__builtin_constant_p (__size))
218 return __getcwd_chk (__buf, __size, __glibc_objsize (__buf));
219
220 if (__size > __glibc_objsize (__buf))
221 return __getcwd_chk_warn (__buf, __size, __glibc_objsize (__buf));
222 }
223 return __getcwd_alias (__buf, __size);
224}
225
226#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
227extern char *__getwd_chk (char *__buf, size_t buflen)
228 __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
229extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
230 __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
231 "doesn't specify buffer size");
232
233__fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
234__NTH (getwd (char *__buf))
235{
236 if (__glibc_objsize (__buf) != (size_t) -1)
237 return __getwd_chk (__buf, __glibc_objsize (__buf));
238 return __getwd_warn (__buf);
239}
240#endif
241
242extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
243 size_t __buflen) __THROW
244 __attr_access ((__write_only__, 2, 3));
245extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
246 size_t __len), confstr)
247 __attr_access ((__write_only__, 2, 3));
248extern size_t __REDIRECT_NTH (__confstr_chk_warn,
249 (int __name, char *__buf, size_t __len,
250 size_t __buflen), __confstr_chk)
251 __warnattr ("confstr called with bigger length than size of destination "
252 "buffer");
253
254__fortify_function size_t
255__NTH (confstr (int __name, char *__buf, size_t __len))
256{
257 if (__glibc_objsize (__buf) != (size_t) -1)
258 {
259 if (!__builtin_constant_p (__len))
260 return __confstr_chk (__name, __buf, __len, __glibc_objsize (__buf));
261
262 if (__glibc_objsize (__buf) < __len)
263 return __confstr_chk_warn (__name, __buf, __len,
264 __glibc_objsize (__buf));
265 }
266 return __confstr_alias (__name, __buf, __len);
267}
268
269
270extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
271 __THROW __wur __attr_access ((__write_only__, 2, 1));
272extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
273 getgroups) __wur __attr_access ((__write_only__, 2, 1));
274extern int __REDIRECT_NTH (__getgroups_chk_warn,
275 (int __size, __gid_t __list[], size_t __listlen),
276 __getgroups_chk)
277 __wur __warnattr ("getgroups called with bigger group count than what "
278 "can fit into destination buffer");
279
280__fortify_function int
281__NTH (getgroups (int __size, __gid_t __list[]))
282{
283 if (__glibc_objsize (__list) != (size_t) -1)
284 {
285 if (!__builtin_constant_p (__size) || __size < 0)
286 return __getgroups_chk (__size, __list, __glibc_objsize (__list));
287
288 if (__size * sizeof (__gid_t) > __glibc_objsize (__list))
289 return __getgroups_chk_warn (__size, __list, __glibc_objsize (__list));
290 }
291 return __getgroups_alias (__size, __list);
292}
293
294
295extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
296 size_t __nreal) __THROW __nonnull ((2))
297 __attr_access ((__write_only__, 2, 3));
298extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
299 size_t __buflen), ttyname_r)
300 __nonnull ((2));
301extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
302 (int __fd, char *__buf, size_t __buflen,
303 size_t __nreal), __ttyname_r_chk)
304 __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
305 "size of destination buffer");
306
307__fortify_function int
308__NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
309{
310 if (__glibc_objsize (__buf) != (size_t) -1)
311 {
312 if (!__builtin_constant_p (__buflen))
313 return __ttyname_r_chk (__fd, __buf, __buflen,
314 __glibc_objsize (__buf));
315
316 if (__buflen > __glibc_objsize (__buf))
317 return __ttyname_r_chk_warn (__fd, __buf, __buflen,
318 __glibc_objsize (__buf));
319 }
320 return __ttyname_r_alias (__fd, __buf, __buflen);
321}
322
323
324#ifdef __USE_POSIX199506
325extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
326 __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
327extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
328 getlogin_r) __nonnull ((1));
329extern int __REDIRECT (__getlogin_r_chk_warn,
330 (char *__buf, size_t __buflen, size_t __nreal),
331 __getlogin_r_chk)
332 __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
333 "size of destination buffer");
334
335__fortify_function int
336getlogin_r (char *__buf, size_t __buflen)
337{
338 if (__glibc_objsize (__buf) != (size_t) -1)
339 {
340 if (!__builtin_constant_p (__buflen))
341 return __getlogin_r_chk (__buf, __buflen, __glibc_objsize (__buf));
342
343 if (__buflen > __glibc_objsize (__buf))
344 return __getlogin_r_chk_warn (__buf, __buflen,
345 __glibc_objsize (__buf));
346 }
347 return __getlogin_r_alias (__buf, __buflen);
348}
349#endif
350
351
352#if defined __USE_MISC || defined __USE_UNIX98
353extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
354 __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
355extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
356 gethostname)
357 __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
358extern int __REDIRECT_NTH (__gethostname_chk_warn,
359 (char *__buf, size_t __buflen, size_t __nreal),
360 __gethostname_chk)
361 __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
362 "size of destination buffer");
363
364__fortify_function int
365__NTH (gethostname (char *__buf, size_t __buflen))
366{
367 if (__glibc_objsize (__buf) != (size_t) -1)
368 {
369 if (!__builtin_constant_p (__buflen))
370 return __gethostname_chk (__buf, __buflen, __glibc_objsize (__buf));
371
372 if (__buflen > __glibc_objsize (__buf))
373 return __gethostname_chk_warn (__buf, __buflen,
374 __glibc_objsize (__buf));
375 }
376 return __gethostname_alias (__buf, __buflen);
377}
378#endif
379
380
381#if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
382extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
383 __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
384extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
385 size_t __buflen),
386 getdomainname) __nonnull ((1))
387 __wur __attr_access ((__write_only__, 1, 2));
388extern int __REDIRECT_NTH (__getdomainname_chk_warn,
389 (char *__buf, size_t __buflen, size_t __nreal),
390 __getdomainname_chk)
391 __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
392 "buflen than size of destination "
393 "buffer");
394
395__fortify_function int
396__NTH (getdomainname (char *__buf, size_t __buflen))
397{
398 if (__glibc_objsize (__buf) != (size_t) -1)
399 {
400 if (!__builtin_constant_p (__buflen))
401 return __getdomainname_chk (__buf, __buflen, __glibc_objsize (__buf));
402
403 if (__buflen > __glibc_objsize (__buf))
404 return __getdomainname_chk_warn (__buf, __buflen,
405 __glibc_objsize (__buf));
406 }
407 return __getdomainname_alias (__buf, __buflen);
408}
409#endif
410