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* _Nonnull, size_t, size_t, int, const struct sockaddr* _Nullable,
34         socklen_t) __INTRODUCED_IN(26);
35 ssize_t __recvfrom_chk(int, void* _Nullable, size_t, size_t, int, struct sockaddr* _Nullable, socklen_t* _Nullable);
36 
37 #if defined(__BIONIC_FORTIFY)
38 
39 __BIONIC_FORTIFY_INLINE
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)40 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)
41     __overloadable
42     __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
43                      "'recvfrom' called with size bigger than buffer") {
44 #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
45   size_t bos = __bos0(buf);
46 
47   if (!__bos_trivially_ge(bos, len)) {
48     return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
49   }
50 #endif
51   return __call_bypassing_fortify(recvfrom)(fd, buf, len, flags, src_addr, addr_len);
52 }
53 
54 __BIONIC_FORTIFY_INLINE
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)55 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)
56     __overloadable
57     __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
58                      "'sendto' called with size bigger than buffer") {
59 #if __ANDROID_API__ >= 26 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
60   size_t bos = __bos0(buf);
61 
62   if (!__bos_trivially_ge(bos, len)) {
63     return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len);
64   }
65 #endif
66   return __call_bypassing_fortify(sendto)(fd, buf, len, flags, dest_addr, addr_len);
67 }
68 
69 __BIONIC_FORTIFY_INLINE
recv(int socket,void * _Nullable const buf __pass_object_size0,size_t len,int flags)70 ssize_t recv(int socket, void* _Nullable const buf __pass_object_size0, size_t len, int flags)
71     __overloadable
72     __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
73                      "'recv' called with size bigger than buffer") {
74   return recvfrom(socket, buf, len, flags, NULL, 0);
75 }
76 
77 __BIONIC_FORTIFY_INLINE
send(int socket,const void * _Nonnull const buf __pass_object_size0,size_t len,int flags)78 ssize_t send(int socket, const void* _Nonnull const buf __pass_object_size0, size_t len, int flags)
79     __overloadable
80     __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
81                      "'send' called with size bigger than buffer") {
82   return sendto(socket, buf, len, flags, NULL, 0);
83 }
84 
85 #endif /* __BIONIC_FORTIFY */
86