George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef _SYS_SOCKET_H_ |
| 30 | #error "Never include this file directly; instead, include <sys/socket.h>" |
| 31 | #endif |
| 32 | |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 33 | extern ssize_t __sendto_chk(int, const void* _Nonnull, size_t, size_t, int, const struct sockaddr* _Nullable, |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 34 | socklen_t) __INTRODUCED_IN(26); |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 35 | ssize_t __recvfrom_chk(int, void* _Nullable, size_t, size_t, int, struct sockaddr* _Nullable, |
| 36 | socklen_t* _Nullable) __INTRODUCED_IN(21); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 37 | |
| 38 | #if defined(__BIONIC_FORTIFY) |
| 39 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 40 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 41 | ssize_t recvfrom(int fd, void* _Nullable const buf __pass_object_size0, size_t len, int flags, struct sockaddr* _Nullable src_addr, socklen_t* _Nullable addr_len) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 42 | __overloadable |
George Burgess IV | ff71793 | 2019-05-09 13:54:22 -0700 | [diff] [blame] | 43 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), |
| 44 | "'recvfrom' called with size bigger than buffer") { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 45 | #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 46 | size_t bos = __bos0(buf); |
| 47 | |
George Burgess IV | d0a0f72 | 2019-09-18 16:53:57 -0700 | [diff] [blame] | 48 | if (!__bos_trivially_ge(bos, len)) { |
| 49 | return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 50 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 51 | #endif |
George Burgess IV | d0a0f72 | 2019-09-18 16:53:57 -0700 | [diff] [blame] | 52 | return __call_bypassing_fortify(recvfrom)(fd, buf, len, flags, src_addr, addr_len); |
| 53 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 54 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 55 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 56 | ssize_t sendto(int fd, const void* _Nonnull const buf __pass_object_size0, size_t len, int flags, const struct sockaddr* _Nullable dest_addr, socklen_t addr_len) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 57 | __overloadable |
George Burgess IV | ff71793 | 2019-05-09 13:54:22 -0700 | [diff] [blame] | 58 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), |
| 59 | "'sendto' called with size bigger than buffer") { |
Logan Chien | d98dac41 | 2019-11-20 09:52:06 -0800 | [diff] [blame] | 60 | #if __ANDROID_API__ >= 26 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 61 | size_t bos = __bos0(buf); |
| 62 | |
George Burgess IV | d0a0f72 | 2019-09-18 16:53:57 -0700 | [diff] [blame] | 63 | if (!__bos_trivially_ge(bos, len)) { |
| 64 | return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 65 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 66 | #endif |
George Burgess IV | d0a0f72 | 2019-09-18 16:53:57 -0700 | [diff] [blame] | 67 | return __call_bypassing_fortify(sendto)(fd, buf, len, flags, dest_addr, addr_len); |
| 68 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 69 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 70 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 71 | ssize_t recv(int socket, void* _Nullable const buf __pass_object_size0, size_t len, int flags) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 72 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 73 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 74 | "'recv' called with size bigger than buffer") { |
| 75 | return recvfrom(socket, buf, len, flags, NULL, 0); |
| 76 | } |
| 77 | |
| 78 | __BIONIC_FORTIFY_INLINE |
zijunzhao | 1e28d06 | 2023-06-13 21:14:33 +0000 | [diff] [blame] | 79 | ssize_t send(int socket, const void* _Nonnull const buf __pass_object_size0, size_t len, int flags) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 80 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 81 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 82 | "'send' called with size bigger than buffer") { |
| 83 | return sendto(socket, buf, len, flags, NULL, 0); |
| 84 | } |
| 85 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 86 | #endif /* __BIONIC_FORTIFY */ |