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 | |
| 33 | extern ssize_t __sendto_chk(int, const void*, size_t, size_t, int, const struct sockaddr*, |
| 34 | socklen_t) __INTRODUCED_IN(26); |
| 35 | ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, struct sockaddr*, |
| 36 | socklen_t*) __INTRODUCED_IN(21); |
| 37 | |
| 38 | #if defined(__BIONIC_FORTIFY) |
| 39 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 40 | #define __recvfrom_bad_size "'recvfrom' called with size bigger than buffer" |
| 41 | #define __sendto_bad_size "'sendto' called with size bigger than buffer" |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 42 | #if defined(__clang__) |
| 43 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 44 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 45 | ssize_t recvfrom(int fd, void* const buf __pass_object_size0, size_t len, int flags, struct sockaddr* src_addr, socklen_t* addr_len) |
| 46 | __overloadable |
| 47 | __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && __bos(buf) < len, |
| 48 | __recvfrom_bad_size) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 49 | size_t bos = __bos0(buf); |
| 50 | |
| 51 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 52 | return __call_bypassing_fortify(recvfrom)(fd, buf, len, flags, src_addr, addr_len); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 53 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 54 | return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len); |
| 55 | } |
| 56 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
| 57 | |
| 58 | #if __ANDROID_API__ >= __ANDROID_API_N_MR1__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 59 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 60 | ssize_t sendto(int fd, const void* const buf __pass_object_size0, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addr_len) |
| 61 | __overloadable |
| 62 | __clang_error_if(__bos0(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && __bos0(buf) < len, |
| 63 | __sendto_bad_size) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 64 | size_t bos = __bos0(buf); |
| 65 | |
| 66 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 67 | return __call_bypassing_fortify(sendto)(fd, buf, len, flags, dest_addr, addr_len); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 68 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 69 | return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len); |
| 70 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 71 | #endif /* __ANDROID_API__ >= __ANDROID_API_N_MR1__ */ |
| 72 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 73 | __BIONIC_FORTIFY_INLINE |
| 74 | ssize_t recv(int socket, void* const buf __pass_object_size0, size_t len, int flags) |
| 75 | __overloadable |
| 76 | __clang_error_if(__bos0(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && __bos0(buf) < len, |
| 77 | "'recv' called with size bigger than buffer") { |
| 78 | return recvfrom(socket, buf, len, flags, NULL, 0); |
| 79 | } |
| 80 | |
| 81 | __BIONIC_FORTIFY_INLINE |
| 82 | ssize_t send(int socket, const void* const buf __pass_object_size0, size_t len, int flags) |
| 83 | __overloadable |
| 84 | __clang_error_if(__bos0(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && __bos0(buf) < len, |
| 85 | "'send' called with size bigger than buffer") { |
| 86 | return sendto(socket, buf, len, flags, NULL, 0); |
| 87 | } |
| 88 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 89 | #else /* defined(__clang__) */ |
| 90 | ssize_t __recvfrom_real(int, void*, size_t, int, struct sockaddr*, socklen_t*) __RENAME(recvfrom); |
| 91 | __errordecl(__recvfrom_error, __recvfrom_bad_size); |
| 92 | |
| 93 | extern ssize_t __sendto_real(int, const void*, size_t, int, const struct sockaddr*, socklen_t) |
| 94 | __RENAME(sendto); |
| 95 | __errordecl(__sendto_error, __sendto_bad_size); |
| 96 | |
| 97 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
| 98 | __BIONIC_FORTIFY_INLINE |
| 99 | ssize_t recvfrom(int fd, void* buf, size_t len, int flags, |
| 100 | struct sockaddr* src_addr, socklen_t* addr_len) { |
| 101 | size_t bos = __bos0(buf); |
| 102 | |
| 103 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 104 | return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len); |
| 105 | } |
| 106 | |
| 107 | if (__builtin_constant_p(len) && (len <= bos)) { |
| 108 | return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len); |
| 109 | } |
| 110 | |
| 111 | if (__builtin_constant_p(len) && (len > bos)) { |
| 112 | __recvfrom_error(); |
| 113 | } |
| 114 | |
| 115 | return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len); |
| 116 | } |
| 117 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
| 118 | |
| 119 | #if __ANDROID_API__ >= __ANDROID_API_N_MR1__ |
| 120 | __BIONIC_FORTIFY_INLINE |
| 121 | ssize_t sendto(int fd, const void* buf, size_t len, int flags, |
| 122 | const struct sockaddr* dest_addr, socklen_t addr_len) { |
| 123 | size_t bos = __bos0(buf); |
| 124 | |
| 125 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 126 | return __sendto_real(fd, buf, len, flags, dest_addr, addr_len); |
| 127 | } |
| 128 | |
| 129 | if (__builtin_constant_p(len) && (len <= bos)) { |
| 130 | return __sendto_real(fd, buf, len, flags, dest_addr, addr_len); |
| 131 | } |
| 132 | |
| 133 | if (__builtin_constant_p(len) && (len > bos)) { |
| 134 | __sendto_error(); |
| 135 | } |
| 136 | |
| 137 | return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len); |
| 138 | } |
| 139 | #endif /* __ANDROID_API__ >= __ANDROID_API_N_MR1__ */ |
| 140 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 141 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 142 | ssize_t recv(int socket, void* buf, size_t len, int flags) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 143 | return recvfrom(socket, buf, len, flags, NULL, 0); |
| 144 | } |
| 145 | |
| 146 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 147 | ssize_t send(int socket, const void* buf, size_t len, int flags) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 148 | return sendto(socket, buf, len, flags, NULL, 0); |
| 149 | } |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 150 | #endif /* defined(__clang__) */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 151 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame^] | 152 | #undef __recvfrom_bad_size |
| 153 | #undef __sendto_bad_size |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 154 | #endif /* __BIONIC_FORTIFY */ |