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 _FCNTL_H
30 #error "Never include this file directly; instead, include <fcntl.h>"
31 #endif
32
33 int __open_2(const char* _Nonnull, int);
34 int __openat_2(int, const char* _Nonnull, int);
35 /*
36 * These are the easiest way to call the real open even in clang FORTIFY.
37 */
38 int __open_real(const char* _Nonnull, int, ...) __RENAME(open);
39 int __openat_real(int, const char* _Nonnull, int, ...) __RENAME(openat);
40
41 #if defined(__BIONIC_FORTIFY)
42 #define __open_too_many_args_error "too many arguments"
43 #define __open_too_few_args_error "called with O_CREAT or O_TMPFILE, but missing mode"
44 #define __open_useless_modes_warning "has superfluous mode bits; missing O_CREAT or O_TMPFILE?"
45 /* O_TMPFILE shares bits with O_DIRECTORY. */
46 #define __open_modes_useful(flags) (((flags) & O_CREAT) || ((flags) & O_TMPFILE) == O_TMPFILE)
47
48 __BIONIC_ERROR_FUNCTION_VISIBILITY
49 int open(const char* _Nonnull pathname, int flags, mode_t modes, ...) __overloadable
50 __errorattr(__open_too_many_args_error);
51
52 /*
53 * pass_object_size serves two purposes here, neither of which involve __bos: it
54 * disqualifies this function from having its address taken (so &open works),
55 * and it makes overload resolution prefer open(const char *, int) over
56 * open(const char *, int, ...).
57 */
58 __BIONIC_FORTIFY_INLINE
open(const char * _Nonnull const __pass_object_size pathname,int flags)59 int open(const char* _Nonnull const __pass_object_size pathname, int flags)
60 __overloadable
61 __clang_error_if(__open_modes_useful(flags), "'open' " __open_too_few_args_error) {
62 #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
63 return __open_2(pathname, flags);
64 #else
65 return __open_real(pathname, flags);
66 #endif
67 }
68
69 __BIONIC_FORTIFY_INLINE
open(const char * _Nonnull const __pass_object_size pathname,int flags,mode_t modes)70 int open(const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
71 __overloadable
72 __clang_warning_if(!__open_modes_useful(flags) && modes,
73 "'open' " __open_useless_modes_warning) {
74 return __open_real(pathname, flags, modes);
75 }
76
77 __BIONIC_ERROR_FUNCTION_VISIBILITY
78 int openat(int dirfd, const char* _Nonnull pathname, int flags, mode_t modes, ...)
79 __overloadable
80 __errorattr(__open_too_many_args_error);
81
82 __BIONIC_FORTIFY_INLINE
openat(int dirfd,const char * _Nonnull const __pass_object_size pathname,int flags)83 int openat(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags)
84 __overloadable
85 __clang_error_if(__open_modes_useful(flags), "'openat' " __open_too_few_args_error) {
86 #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
87 return __openat_2(dirfd, pathname, flags);
88 #else
89 return __openat_real(dirfd, pathname, flags);
90 #endif
91 }
92
93 __BIONIC_FORTIFY_INLINE
openat(int dirfd,const char * _Nonnull const __pass_object_size pathname,int flags,mode_t modes)94 int openat(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
95 __overloadable
96 __clang_warning_if(!__open_modes_useful(flags) && modes,
97 "'openat' " __open_useless_modes_warning) {
98 return __openat_real(dirfd, pathname, flags, modes);
99 }
100
101 /* Note that open == open64, so we reuse those bits in the open64 variants below. */
102
103 __BIONIC_ERROR_FUNCTION_VISIBILITY
104 int open64(const char* _Nonnull pathname, int flags, mode_t modes, ...) __overloadable
105 __errorattr(__open_too_many_args_error);
106
107 __BIONIC_FORTIFY_INLINE
open64(const char * _Nonnull const __pass_object_size pathname,int flags)108 int open64(const char* _Nonnull const __pass_object_size pathname, int flags)
109 __overloadable
110 __clang_error_if(__open_modes_useful(flags), "'open64' " __open_too_few_args_error) {
111 return open(pathname, flags);
112 }
113
114 __BIONIC_FORTIFY_INLINE
open64(const char * _Nonnull const __pass_object_size pathname,int flags,mode_t modes)115 int open64(const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
116 __overloadable
117 __clang_warning_if(!__open_modes_useful(flags) && modes,
118 "'open64' " __open_useless_modes_warning) {
119 return open(pathname, flags, modes);
120 }
121
122 __BIONIC_ERROR_FUNCTION_VISIBILITY
123 int openat64(int dirfd, const char* _Nonnull pathname, int flags, mode_t modes, ...)
124 __overloadable
125 __errorattr(__open_too_many_args_error);
126
127 __BIONIC_FORTIFY_INLINE
openat64(int dirfd,const char * _Nonnull const __pass_object_size pathname,int flags)128 int openat64(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags)
129 __overloadable
130 __clang_error_if(__open_modes_useful(flags), "'openat64' " __open_too_few_args_error) {
131 return openat(dirfd, pathname, flags);
132 }
133
134 __BIONIC_FORTIFY_INLINE
openat64(int dirfd,const char * _Nonnull const __pass_object_size pathname,int flags,mode_t modes)135 int openat64(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
136 __overloadable
137 __clang_warning_if(!__open_modes_useful(flags) && modes,
138 "'openat64' " __open_useless_modes_warning) {
139 return openat(dirfd, pathname, flags, modes);
140 }
141
142 #undef __open_too_many_args_error
143 #undef __open_too_few_args_error
144 #undef __open_useless_modes_warning
145 #undef __open_modes_useful
146 #endif /* defined(__BIONIC_FORTIFY) */
147