blob: 73cf973b8d69d46767c70c939c5cb4891fe12af9 [file] [log] [blame]
Elliott Hughesb83d6742016-02-25 20:33:47 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 1988 Regents of the University of California.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
Elliott Hughesb83d6742016-02-25 20:33:47 -080058#include <poll.h>
59#include <stdarg.h>
60#include <stddef.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <sys/cdefs.h>
65#include <sys/select.h>
66#include <sys/socket.h>
67#include <sys/stat.h>
68#include <unistd.h>
69
70#include "private/bionic_fortify.h"
71
72//
Elliott Hughesef2b2fe2017-04-16 08:50:58 -070073// For more about FORTIFY see:
74//
75// https://android-developers.googleblog.com/2017/04/fortify-in-android.html
76//
Elliott Hughesb83d6742016-02-25 20:33:47 -080077// http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
78// http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
79//
Elliott Hughesef2b2fe2017-04-16 08:50:58 -070080
Elliott Hughesa6714d12017-12-04 14:16:38 -080081int __FD_ISSET_chk(int fd, const fd_set* set, size_t set_size) {
Elliott Hughesb83d6742016-02-25 20:33:47 -080082 __check_fd_set("FD_ISSET", fd, set_size);
Andreas Gampe00a6d5f2018-04-13 13:52:10 -070083 return __FD_ISSET(fd, set);
Elliott Hughesb83d6742016-02-25 20:33:47 -080084}
85
86void __FD_CLR_chk(int fd, fd_set* set, size_t set_size) {
87 __check_fd_set("FD_CLR", fd, set_size);
Andreas Gampe00a6d5f2018-04-13 13:52:10 -070088 __FD_CLR(fd, set);
Elliott Hughesb83d6742016-02-25 20:33:47 -080089}
90
91void __FD_SET_chk(int fd, fd_set* set, size_t set_size) {
92 __check_fd_set("FD_SET", fd, set_size);
Andreas Gampe00a6d5f2018-04-13 13:52:10 -070093 __FD_SET(fd, set);
Elliott Hughesb83d6742016-02-25 20:33:47 -080094}
95
96char* __fgets_chk(char* dst, int supplied_size, FILE* stream, size_t dst_len_from_compiler) {
Elliott Hughesb83d6742016-02-25 20:33:47 -080097 __check_buffer_access("fgets", "write into", supplied_size, dst_len_from_compiler);
98 return fgets(dst, supplied_size, stream);
99}
100
Elliott Hughesec6850d2017-08-01 08:28:46 -0700101size_t __fread_chk(void* buf, size_t size, size_t count, FILE* stream, size_t buf_size) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800102 size_t total;
103 if (__predict_false(__size_mul_overflow(size, count, &total))) {
104 // overflow: trigger the error path in fread
105 return fread(buf, size, count, stream);
106 }
107 __check_buffer_access("fread", "write into", total, buf_size);
108 return fread(buf, size, count, stream);
109}
110
Elliott Hughesec6850d2017-08-01 08:28:46 -0700111size_t __fwrite_chk(const void* buf, size_t size, size_t count, FILE* stream, size_t buf_size) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800112 size_t total;
113 if (__predict_false(__size_mul_overflow(size, count, &total))) {
114 // overflow: trigger the error path in fwrite
115 return fwrite(buf, size, count, stream);
116 }
117 __check_buffer_access("fwrite", "read from", total, buf_size);
118 return fwrite(buf, size, count, stream);
119}
120
121extern char* __getcwd_chk(char* buf, size_t len, size_t actual_size) {
122 __check_buffer_access("getcwd", "write into", len, actual_size);
123 return getcwd(buf, len);
124}
125
126void* __memchr_chk(const void* s, int c, size_t n, size_t actual_size) {
127 __check_buffer_access("memchr", "read from", n, actual_size);
George Burgess IVbd3d2082017-04-04 17:34:02 -0700128 return const_cast<void*>(memchr(s, c, n));
Elliott Hughesb83d6742016-02-25 20:33:47 -0800129}
130
131// Runtime implementation of __builtin____memmove_chk (used directly by compiler, not in headers).
132extern "C" void* __memmove_chk(void* dst, const void* src, size_t len, size_t dst_len) {
133 __check_buffer_access("memmove", "write into", len, dst_len);
134 return memmove(dst, src, len);
135}
136
Elliott Hughes3c6016f2016-03-01 14:45:58 -0800137// memcpy is performance-critical enough that we have assembler __memcpy_chk implementations.
138// This function is used to give better diagnostics than we can easily do from assembler.
139extern "C" void* __memcpy_chk_fail(void* /*dst*/, const void* /*src*/, size_t count, size_t dst_len) {
140 __check_count("memcpy", "count", count);
141 __check_buffer_access("memcpy", "write into", count, dst_len);
142 abort(); // One of the above is supposed to have failed, otherwise we shouldn't have been called.
143}
144
Elliott Hughesb83d6742016-02-25 20:33:47 -0800145void* __memrchr_chk(const void* s, int c, size_t n, size_t actual_size) {
146 __check_buffer_access("memrchr", "read from", n, actual_size);
Elliott Hughesdf9a4892017-08-23 14:34:03 -0700147 return memrchr(const_cast<void *>(s), c, n);
Elliott Hughesb83d6742016-02-25 20:33:47 -0800148}
149
Elliott Hughes62e59642016-03-01 11:22:42 -0800150// memset is performance-critical enough that we have assembler __memset_chk implementations.
151// This function is used to give better diagnostics than we can easily do from assembler.
152extern "C" void* __memset_chk_fail(void* /*dst*/, int /*byte*/, size_t count, size_t dst_len) {
153 __check_count("memset", "count", count);
154 __check_buffer_access("memset", "write into", count, dst_len);
155 abort(); // One of the above is supposed to have failed, otherwise we shouldn't have been called.
156}
157
Elliott Hughesb83d6742016-02-25 20:33:47 -0800158int __poll_chk(pollfd* fds, nfds_t fd_count, int timeout, size_t fds_size) {
159 __check_pollfd_array("poll", fds_size, fd_count);
160 return poll(fds, fd_count, timeout);
161}
162
163int __ppoll_chk(pollfd* fds, nfds_t fd_count, const timespec* timeout,
164 const sigset_t* mask, size_t fds_size) {
165 __check_pollfd_array("ppoll", fds_size, fd_count);
166 return ppoll(fds, fd_count, timeout, mask);
167}
168
Elliott Hughesb83bf142018-03-22 11:01:25 -0700169int __ppoll64_chk(pollfd* fds, nfds_t fd_count, const timespec* timeout,
170 const sigset64_t* mask, size_t fds_size) {
171 __check_pollfd_array("ppoll64", fds_size, fd_count);
172 return ppoll64(fds, fd_count, timeout, mask);
173}
174
Elliott Hughesb83d6742016-02-25 20:33:47 -0800175ssize_t __pread64_chk(int fd, void* buf, size_t count, off64_t offset, size_t buf_size) {
176 __check_count("pread64", "count", count);
177 __check_buffer_access("pread64", "write into", count, buf_size);
178 return pread64(fd, buf, count, offset);
179}
180
181ssize_t __pread_chk(int fd, void* buf, size_t count, off_t offset, size_t buf_size) {
182 __check_count("pread", "count", count);
183 __check_buffer_access("pread", "write into", count, buf_size);
184 return pread(fd, buf, count, offset);
185}
186
187ssize_t __pwrite64_chk(int fd, const void* buf, size_t count, off64_t offset,
188 size_t buf_size) {
189 __check_count("pwrite64", "count", count);
190 __check_buffer_access("pwrite64", "read from", count, buf_size);
191 return pwrite64(fd, buf, count, offset);
192}
193
194ssize_t __pwrite_chk(int fd, const void* buf, size_t count, off_t offset,
195 size_t buf_size) {
196 __check_count("pwrite", "count", count);
197 __check_buffer_access("pwrite", "read from", count, buf_size);
198 return pwrite(fd, buf, count, offset);
199}
200
201ssize_t __read_chk(int fd, void* buf, size_t count, size_t buf_size) {
202 __check_count("read", "count", count);
203 __check_buffer_access("read", "write into", count, buf_size);
204 return read(fd, buf, count);
205}
206
207ssize_t __readlinkat_chk(int dirfd, const char* path, char* buf, size_t size, size_t buf_size) {
208 __check_count("readlinkat", "size", size);
209 __check_buffer_access("readlinkat", "write into", size, buf_size);
210 return readlinkat(dirfd, path, buf, size);
211}
212
213ssize_t __readlink_chk(const char* path, char* buf, size_t size, size_t buf_size) {
214 __check_count("readlink", "size", size);
215 __check_buffer_access("readlink", "write into", size, buf_size);
216 return readlink(path, buf, size);
217}
218
219ssize_t __recvfrom_chk(int socket, void* buf, size_t len, size_t buf_size,
Elliott Hughes8197aca2016-08-12 09:20:07 -0700220 int flags, sockaddr* src_addr, socklen_t* addrlen) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800221 __check_buffer_access("recvfrom", "write into", len, buf_size);
222 return recvfrom(socket, buf, len, flags, src_addr, addrlen);
223}
224
Daniel Micay95b59c52017-02-13 17:27:59 -0800225ssize_t __sendto_chk(int socket, const void* buf, size_t len, size_t buflen,
226 int flags, const struct sockaddr* dest_addr,
227 socklen_t addrlen) {
228 __check_buffer_access("sendto", "read from", len, buflen);
229 return sendto(socket, buf, len, flags, dest_addr, addrlen);
230}
231
Elliott Hughesb83d6742016-02-25 20:33:47 -0800232// Runtime implementation of __builtin____stpcpy_chk (used directly by compiler, not in headers)..
233extern "C" char* __stpcpy_chk(char* dst, const char* src, size_t dst_len) {
234 // TODO: optimize so we don't scan src twice.
235 size_t src_len = strlen(src) + 1;
236 __check_buffer_access("stpcpy", "write into", src_len, dst_len);
237 return stpcpy(dst, src);
238}
239
240// Runtime implementation of __builtin____stpncpy_chk (used directly by compiler, not in headers).
Elliott Hughesec6850d2017-08-01 08:28:46 -0700241extern "C" char* __stpncpy_chk(char* dst, const char* src, size_t len, size_t dst_len) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800242 __check_buffer_access("stpncpy", "write into", len, dst_len);
243 return stpncpy(dst, src, len);
244}
245
246// This is a variant of __stpncpy_chk, but it also checks to make
247// sure we don't read beyond the end of "src". The code for this is
248// based on the original version of stpncpy, but modified to check
Peter Collingbournea51bded2019-08-08 18:47:22 -0700249// how much we read from "src" during the copy operation.
Elliott Hughesec6850d2017-08-01 08:28:46 -0700250char* __stpncpy_chk2(char* dst, const char* src, size_t n, size_t dst_len, size_t src_len) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800251 __check_buffer_access("stpncpy", "write into", n, dst_len);
252 if (n != 0) {
253 char* d = dst;
254 const char* s = src;
255
256 do {
Peter Collingbournea51bded2019-08-08 18:47:22 -0700257 size_t s_copy_len = static_cast<size_t>(s - src);
258 if (__predict_false(s_copy_len >= src_len)) {
259 __fortify_fatal("stpncpy: detected read past end of %zu-byte buffer", src_len);
260 }
261
Elliott Hughesb83d6742016-02-25 20:33:47 -0800262 if ((*d++ = *s++) == 0) {
263 // NUL pad the remaining n-1 bytes.
264 while (--n != 0) {
265 *d++ = 0;
266 }
267 break;
268 }
269 } while (--n != 0);
Elliott Hughesb83d6742016-02-25 20:33:47 -0800270 }
271
272 return dst;
273}
274
Elliott Hughesc75da092016-05-25 17:01:31 -0700275// strcat is performance-critical enough that we have assembler __strcat_chk implementations.
276// This function is used to give better diagnostics than we can easily do from assembler.
277extern "C" void __strcat_chk_fail(size_t dst_buf_size) {
278 __fortify_fatal("strcat: prevented write past end of %zu-byte buffer", dst_buf_size);
279}
280
Elliott Hughesb83d6742016-02-25 20:33:47 -0800281char* __strchr_chk(const char* p, int ch, size_t s_len) {
282 for (;; ++p, s_len--) {
283 if (__predict_false(s_len == 0)) {
284 __fortify_fatal("strchr: prevented read past end of buffer");
285 }
286 if (*p == static_cast<char>(ch)) {
287 return const_cast<char*>(p);
288 }
289 if (*p == '\0') {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700290 return nullptr;
Elliott Hughesb83d6742016-02-25 20:33:47 -0800291 }
292 }
293}
294
Elliott Hughesbdd8f892016-05-26 16:38:34 -0700295// strcpy is performance-critical enough that we have assembler __strcpy_chk implementations.
296// This function is used to give better diagnostics than we can easily do from assembler.
297extern "C" void __strcpy_chk_fail(size_t dst_buf_size) {
298 __fortify_fatal("strcpy: prevented write past end of %zu-byte buffer", dst_buf_size);
299}
300
Elliott Hughesb83d6742016-02-25 20:33:47 -0800301size_t __strlcat_chk(char* dst, const char* src,
302 size_t supplied_size, size_t dst_len_from_compiler) {
303 __check_buffer_access("strlcat", "write into", supplied_size, dst_len_from_compiler);
304 return strlcat(dst, src, supplied_size);
305}
306
307size_t __strlcpy_chk(char* dst, const char* src,
308 size_t supplied_size, size_t dst_len_from_compiler) {
309 __check_buffer_access("strlcpy", "write into", supplied_size, dst_len_from_compiler);
310 return strlcpy(dst, src, supplied_size);
311}
312
313size_t __strlen_chk(const char* s, size_t s_len) {
314 // TODO: "prevented" here would be a lie because this strlen can run off the end.
315 // strlen is too important to be expensive, so we wanted to be able to call the optimized
316 // implementation, but I think we need to implement optimized assembler __strlen_chk routines.
317 size_t ret = strlen(s);
318 if (__predict_false(ret >= s_len)) {
319 __fortify_fatal("strlen: detected read past end of buffer");
320 }
321 return ret;
322}
323
324// Runtime implementation of __builtin____strncat_chk (used directly by compiler, not in headers).
Elliott Hughesec6850d2017-08-01 08:28:46 -0700325extern "C" char* __strncat_chk(char* dst, const char* src, size_t len, size_t dst_buf_size) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800326 if (len == 0) {
327 return dst;
328 }
329
330 size_t dst_len = __strlen_chk(dst, dst_buf_size);
331 char* d = dst + dst_len;
332 dst_buf_size -= dst_len;
333
334 while (*src != '\0') {
335 *d++ = *src++;
336 len--; dst_buf_size--;
337
338 if (__predict_false(dst_buf_size == 0)) {
339 __fortify_fatal("strncat: prevented write past end of buffer");
340 }
341
342 if (len == 0) {
343 break;
344 }
345 }
346
347 *d = '\0';
348 return dst;
349}
350
351// Runtime implementation of __builtin____strncpy_chk (used directly by compiler, not in headers).
Elliott Hughesec6850d2017-08-01 08:28:46 -0700352extern "C" char* __strncpy_chk(char* dst, const char* src, size_t len, size_t dst_len) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800353 __check_buffer_access("strncpy", "write into", len, dst_len);
354 return strncpy(dst, src, len);
355}
356
357// This is a variant of __strncpy_chk, but it also checks to make
358// sure we don't read beyond the end of "src". The code for this is
359// based on the original version of strncpy, but modified to check
Peter Collingbournea51bded2019-08-08 18:47:22 -0700360// how much we read from "src" during the copy operation.
Elliott Hughesec6850d2017-08-01 08:28:46 -0700361char* __strncpy_chk2(char* dst, const char* src, size_t n, size_t dst_len, size_t src_len) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800362 __check_buffer_access("strncpy", "write into", n, dst_len);
363 if (n != 0) {
364 char* d = dst;
365 const char* s = src;
366
367 do {
Peter Collingbournea51bded2019-08-08 18:47:22 -0700368 size_t s_copy_len = static_cast<size_t>(s - src);
369 if (__predict_false(s_copy_len >= src_len)) {
370 __fortify_fatal("strncpy: detected read past end of %zu-byte buffer", src_len);
371 }
372
Elliott Hughesb83d6742016-02-25 20:33:47 -0800373 if ((*d++ = *s++) == 0) {
374 // NUL pad the remaining n-1 bytes.
375 while (--n != 0) {
376 *d++ = 0;
377 }
378 break;
379 }
380 } while (--n != 0);
Elliott Hughesb83d6742016-02-25 20:33:47 -0800381 }
382
383 return dst;
384}
385
386char* __strrchr_chk(const char* p, int ch, size_t s_len) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700387 for (const char* save = nullptr;; ++p, s_len--) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800388 if (s_len == 0) {
389 __fortify_fatal("strrchr: prevented read past end of buffer");
390 }
391 if (*p == static_cast<char>(ch)) {
392 save = p;
393 }
394 if (!*p) {
395 return const_cast<char*>(save);
396 }
397 }
398}
399
400mode_t __umask_chk(mode_t mode) {
401 if (__predict_false((mode & 0777) != mode)) {
402 __fortify_fatal("umask: called with invalid mask %o", mode);
403 }
404
405 return umask(mode);
406}
407
408// Runtime implementation of __builtin____vsnprintf_chk (used directly by compiler, not in headers).
409extern "C" int __vsnprintf_chk(char* dst, size_t supplied_size, int /*flags*/,
410 size_t dst_len_from_compiler, const char* format, va_list va) {
411 __check_buffer_access("vsnprintf", "write into", supplied_size, dst_len_from_compiler);
412 return vsnprintf(dst, supplied_size, format, va);
413}
414
415// Runtime implementation of __builtin____snprintf_chk (used directly by compiler, not in headers).
416extern "C" int __snprintf_chk(char* dst, size_t supplied_size, int flags,
417 size_t dst_len_from_compiler, const char* format, ...) {
418 va_list va;
419 va_start(va, format);
420 int result = __vsnprintf_chk(dst, supplied_size, flags, dst_len_from_compiler, format, va);
421 va_end(va);
422 return result;
423}
424
425// Runtime implementation of __builtin____vsprintf_chk (used directly by compiler, not in headers).
426extern "C" int __vsprintf_chk(char* dst, int /*flags*/,
427 size_t dst_len_from_compiler, const char* format, va_list va) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -0700428 // The compiler uses SIZE_MAX to mean "no idea", but our vsnprintf rejects sizes that large.
429 int result = vsnprintf(dst,
430 dst_len_from_compiler == SIZE_MAX ? SSIZE_MAX : dst_len_from_compiler,
431 format, va);
432
433 // Try to catch failures after the fact...
Elliott Hughesb83d6742016-02-25 20:33:47 -0800434 __check_buffer_access("vsprintf", "write into", result + 1, dst_len_from_compiler);
435 return result;
436}
437
438// Runtime implementation of __builtin____sprintf_chk (used directly by compiler, not in headers).
439extern "C" int __sprintf_chk(char* dst, int flags, size_t dst_len_from_compiler,
440 const char* format, ...) {
441 va_list va;
442 va_start(va, format);
443 int result = __vsprintf_chk(dst, flags, dst_len_from_compiler, format, va);
444 va_end(va);
445 return result;
446}
447
448ssize_t __write_chk(int fd, const void* buf, size_t count, size_t buf_size) {
449 __check_count("write", "count", count);
450 __check_buffer_access("write", "read from", count, buf_size);
451 return write(fd, buf, count);
452}
George Burgess IVd34b0a92017-07-25 11:43:39 -0700453
Haibo Huangf1c8d1a2018-11-27 15:38:47 -0800454#if defined(RENAME___STRCAT_CHK)
455#define __STRCAT_CHK __strcat_chk_generic
456#else
457#define __STRCAT_CHK __strcat_chk
458#endif // RENAME___STRCAT_CHK
459
George Burgess IVd34b0a92017-07-25 11:43:39 -0700460// Runtime implementation of __builtin____strcat_chk (used directly by compiler, not in headers).
Haibo Huangf1c8d1a2018-11-27 15:38:47 -0800461extern "C" char* __STRCAT_CHK(char* dst, const char* src, size_t dst_buf_size) {
George Burgess IVd34b0a92017-07-25 11:43:39 -0700462 char* save = dst;
463 size_t dst_len = __strlen_chk(dst, dst_buf_size);
464
465 dst += dst_len;
466 dst_buf_size -= dst_len;
467
468 while ((*dst++ = *src++) != '\0') {
469 dst_buf_size--;
470 if (__predict_false(dst_buf_size == 0)) {
471 __fortify_fatal("strcat: prevented write past end of %zu-byte buffer", dst_buf_size);
472 }
473 }
474
475 return save;
476}
George Burgess IVd34b0a92017-07-25 11:43:39 -0700477
Haibo Huangf1c8d1a2018-11-27 15:38:47 -0800478#if defined(RENAME___STRCPY_CHK)
479#define __STRCPY_CHK __strcpy_chk_generic
480#else
481#define __STRCPY_CHK __strcpy_chk
482#endif // RENAME___STRCPY_CHK
483
George Burgess IVd34b0a92017-07-25 11:43:39 -0700484// Runtime implementation of __builtin____strcpy_chk (used directly by compiler, not in headers).
Haibo Huangf1c8d1a2018-11-27 15:38:47 -0800485extern "C" char* __STRCPY_CHK(char* dst, const char* src, size_t dst_len) {
George Burgess IVd34b0a92017-07-25 11:43:39 -0700486 // TODO: optimize so we don't scan src twice.
487 size_t src_len = strlen(src) + 1;
488 __check_buffer_access("strcpy", "write into", src_len, dst_len);
489 return strcpy(dst, src);
490}
George Burgess IVd34b0a92017-07-25 11:43:39 -0700491
Elliott Hughes8d9549e2022-12-09 19:58:00 +0000492#if !defined(__arm__) && !defined(__aarch64__)
George Burgess IVd34b0a92017-07-25 11:43:39 -0700493// Runtime implementation of __memcpy_chk (used directly by compiler, not in headers).
Elliott Hughes8d9549e2022-12-09 19:58:00 +0000494// arm32 and arm64 have assembler implementations, and don't need this C fallback.
George Burgess IVd34b0a92017-07-25 11:43:39 -0700495extern "C" void* __memcpy_chk(void* dst, const void* src, size_t count, size_t dst_len) {
496 __check_count("memcpy", "count", count);
497 __check_buffer_access("memcpy", "write into", count, dst_len);
498 return memcpy(dst, src, count);
499}
Elliott Hughes3cc366d2022-11-16 21:07:56 +0000500#endif
George Burgess IV849c0b92019-06-10 16:22:09 -0700501
502// Runtime implementation of __mempcpy_chk (used directly by compiler, not in headers).
503extern "C" void* __mempcpy_chk(void* dst, const void* src, size_t count, size_t dst_len) {
504 __check_count("mempcpy", "count", count);
505 __check_buffer_access("mempcpy", "write into", count, dst_len);
506 return mempcpy(dst, src, count);
507}