blob: c1a85c6f988dc59b98eb9a407d37e5a8803be561 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
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
33extern ssize_t __sendto_chk(int, const void*, size_t, size_t, int, const struct sockaddr*,
34 socklen_t) __INTRODUCED_IN(26);
35ssize_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 IV54f5d832017-07-31 21:21:10 -070040#define __recvfrom_bad_size "'recvfrom' called with size bigger than buffer"
41#define __sendto_bad_size "'sendto' called with size bigger than buffer"
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000042
George Burgess IVb97049c2017-07-24 15:05:05 -070043#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -070044__BIONIC_FORTIFY_INLINE
George Burgess IV54f5d832017-07-31 21:21:10 -070045ssize_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
George Burgess IV5273dc52019-05-09 13:46:57 -070047 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), __recvfrom_bad_size) {
George Burgess IVb97049c2017-07-24 15:05:05 -070048 size_t bos = __bos0(buf);
49
50 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
George Burgess IV54f5d832017-07-31 21:21:10 -070051 return __call_bypassing_fortify(recvfrom)(fd, buf, len, flags, src_addr, addr_len);
George Burgess IVb97049c2017-07-24 15:05:05 -070052 }
George Burgess IVb97049c2017-07-24 15:05:05 -070053 return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
54}
55#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
56
57#if __ANDROID_API__ >= __ANDROID_API_N_MR1__
George Burgess IVb97049c2017-07-24 15:05:05 -070058__BIONIC_FORTIFY_INLINE
George Burgess IV54f5d832017-07-31 21:21:10 -070059ssize_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)
60 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070061 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len), __sendto_bad_size) {
George Burgess IVb97049c2017-07-24 15:05:05 -070062 size_t bos = __bos0(buf);
63
64 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
George Burgess IV54f5d832017-07-31 21:21:10 -070065 return __call_bypassing_fortify(sendto)(fd, buf, len, flags, dest_addr, addr_len);
George Burgess IVb97049c2017-07-24 15:05:05 -070066 }
George Burgess IVb97049c2017-07-24 15:05:05 -070067 return __sendto_chk(fd, buf, len, bos, flags, dest_addr, addr_len);
68}
George Burgess IVb97049c2017-07-24 15:05:05 -070069#endif /* __ANDROID_API__ >= __ANDROID_API_N_MR1__ */
70
George Burgess IV54f5d832017-07-31 21:21:10 -070071__BIONIC_FORTIFY_INLINE
72ssize_t recv(int socket, void* const buf __pass_object_size0, size_t len, int flags)
73 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070074 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
George Burgess IV54f5d832017-07-31 21:21:10 -070075 "'recv' called with size bigger than buffer") {
76 return recvfrom(socket, buf, len, flags, NULL, 0);
77}
78
79__BIONIC_FORTIFY_INLINE
80ssize_t send(int socket, const void* const buf __pass_object_size0, size_t len, int flags)
81 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070082 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
George Burgess IV54f5d832017-07-31 21:21:10 -070083 "'send' called with size bigger than buffer") {
84 return sendto(socket, buf, len, flags, NULL, 0);
85}
86
George Burgess IV54f5d832017-07-31 21:21:10 -070087#undef __recvfrom_bad_size
88#undef __sendto_bad_size
George Burgess IVb97049c2017-07-24 15:05:05 -070089#endif /* __BIONIC_FORTIFY */