1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef _SYS_SOCKET_H 17 #error "Never include this file directly; instead, include <sys/socket.h>" 18 #endif 19 20 #include "fortify.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 ssize_t __sendto_chk(int, const void*, size_t, size_t, int, const struct sockaddr*, 27 socklen_t); 28 ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, struct sockaddr*, 29 socklen_t*); 30 ssize_t __send_chk(int, const void*, size_t, size_t, int); 31 ssize_t __recv_chk(int, void*, size_t, size_t, int); 32 33 34 #ifdef __FORTIFY_COMPILATION 35 __DIAGNOSE_FORTIFY_INLINE 36 ssize_t recvfrom(int fd, void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags, 37 struct sockaddr* src_addr, socklen_t* addr_len) 38 __DIAGNOSE_OVERLOAD 39 __DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len), 40 "'recvfrom' " CALLED_WITH_SIZE_BIGGER_BUFFER) 41 { 42 #ifdef __FORTIFY_RUNTIME 43 size_t bos = __DIAGNOSE_BOS0(buf); 44 45 if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) { 46 return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len); 47 } 48 #endif 49 return __DIAGNOSE_CALL_BYPASSING_FORTIFY(recvfrom)(fd, buf, len, flags, src_addr, addr_len); 50 } 51 52 __DIAGNOSE_FORTIFY_INLINE 53 ssize_t sendto(int fd, const void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags, 54 const struct sockaddr* dest_addr, socklen_t addr_len) 55 __DIAGNOSE_OVERLOAD 56 __DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len), 57 "'sendto' " CALLED_WITH_SIZE_BIGGER_BUFFER) 58 { 59 #ifdef __FORTIFY_RUNTIME 60 size_t bos = __DIAGNOSE_BOS0(buf); 61 62 if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) { 63 return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len); 64 } 65 #endif 66 return __DIAGNOSE_CALL_BYPASSING_FORTIFY(sendto)(fd, buf, len, flags, dest_addr, addr_len); 67 } 68 69 __DIAGNOSE_FORTIFY_INLINE 70 ssize_t recv(int socket, void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags) 71 __DIAGNOSE_OVERLOAD 72 __DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len), 73 "'recv' " CALLED_WITH_SIZE_BIGGER_BUFFER) 74 { 75 #ifdef __FORTIFY_RUNTIME 76 size_t bos = __DIAGNOSE_BOS0(buf); 77 78 if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) { 79 return __recv_chk(socket, buf, len, bos, flags); 80 } 81 #endif 82 return __DIAGNOSE_CALL_BYPASSING_FORTIFY(recv)(socket, buf, len, flags); 83 } 84 85 __DIAGNOSE_FORTIFY_INLINE 86 ssize_t send(int socket, const void* const buf __DIAGNOSE_PASS_OBJECT_SIZE0, size_t len, int flags) 87 __DIAGNOSE_OVERLOAD 88 __DIAGNOSE_ERROR_IF(__DIAGNOSE_UNEVALUATED_LT(__DIAGNOSE_BOS0(buf), len), 89 "'send' " CALLED_WITH_SIZE_BIGGER_BUFFER) 90 { 91 #ifdef __FORTIFY_RUNTIME 92 size_t bos = __DIAGNOSE_BOS0(buf); 93 94 if (!__DIAGNOSE_BOS_TRIVIALLY_GE(bos, len)) { 95 return __send_chk(socket, buf, len, bos, flags); 96 } 97 #endif 98 return __DIAGNOSE_CALL_BYPASSING_FORTIFY(send)(socket, buf, len, flags); 99 } 100 #endif 101 102 #ifdef __cplusplus 103 } 104 #endif