Home | History | Annotate | Download | only in fortify
      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 _POLL_H_
     30 #error "Never include this file directly; instead, include <poll.h>"
     31 #endif
     32 
     33 int __poll_chk(struct pollfd*, nfds_t, int, size_t) __INTRODUCED_IN(23);
     34 int __ppoll_chk(struct pollfd*, nfds_t, const struct timespec*, const sigset_t*, size_t)
     35   __INTRODUCED_IN(23);
     36 
     37 #if defined(__BIONIC_FORTIFY)
     38 #if __ANDROID_API__ >= __ANDROID_API_M__
     39 #if defined(__clang__)
     40 __BIONIC_FORTIFY_INLINE
     41 int poll(struct pollfd* const fds __pass_object_size, nfds_t fd_count, int timeout)
     42     __overloadable
     43     __clang_error_if(__bos(fds) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
     44                        __bos(fds) < sizeof(*fds) * fd_count,
     45                      "in call to 'poll', fd_count is larger than the given buffer") {
     46   size_t bos_fds = __bos(fds);
     47 
     48   if (bos_fds == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
     49     return __call_bypassing_fortify(poll)(fds, fd_count, timeout);
     50   }
     51   return __poll_chk(fds, fd_count, timeout, bos_fds);
     52 }
     53 
     54 __BIONIC_FORTIFY_INLINE
     55 int ppoll(struct pollfd* const fds __pass_object_size, nfds_t fd_count, const struct timespec* timeout, const sigset_t* mask)
     56     __overloadable
     57     __clang_error_if(__bos(fds) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
     58                        __bos(fds) < sizeof(*fds) * fd_count,
     59                      "in call to 'ppoll', fd_count is larger than the given buffer") {
     60   size_t bos_fds = __bos(fds);
     61 
     62   if (bos_fds == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
     63     return __call_bypassing_fortify(ppoll)(fds, fd_count, timeout, mask);
     64   }
     65   return __ppoll_chk(fds, fd_count, timeout, mask, bos_fds);
     66 }
     67 #else /* defined(__clang__) */
     68 int __poll_real(struct pollfd*, nfds_t, int) __RENAME(poll);
     69 __errordecl(__poll_too_small_error, "poll: pollfd array smaller than fd count");
     70 
     71 int __ppoll_real(struct pollfd*, nfds_t, const struct timespec*, const sigset_t*) __RENAME(ppoll)
     72   __INTRODUCED_IN(21);
     73 __errordecl(__ppoll_too_small_error, "ppoll: pollfd array smaller than fd count");
     74 
     75 __BIONIC_FORTIFY_INLINE
     76 int poll(struct pollfd* fds, nfds_t fd_count, int timeout) {
     77   if (__bos(fds) != __BIONIC_FORTIFY_UNKNOWN_SIZE) {
     78     if (!__builtin_constant_p(fd_count)) {
     79       return __poll_chk(fds, fd_count, timeout, __bos(fds));
     80     } else if (__bos(fds) / sizeof(*fds) < fd_count) {
     81       __poll_too_small_error();
     82     }
     83   }
     84   return __poll_real(fds, fd_count, timeout);
     85 }
     86 
     87 __BIONIC_FORTIFY_INLINE
     88 int ppoll(struct pollfd* fds, nfds_t fd_count, const struct timespec* timeout,
     89           const sigset_t* mask) {
     90   if (__bos(fds) != __BIONIC_FORTIFY_UNKNOWN_SIZE) {
     91     if (!__builtin_constant_p(fd_count)) {
     92       return __ppoll_chk(fds, fd_count, timeout, mask, __bos(fds));
     93     } else if (__bos(fds) / sizeof(*fds) < fd_count) {
     94       __ppoll_too_small_error();
     95     }
     96   }
     97   return __ppoll_real(fds, fd_count, timeout, mask);
     98 }
     99 
    100 #endif /* defined(__clang__) */
    101 #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
    102 #endif /* defined(__BIONIC_FORTIFY) */
    103