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 _FCNTL_H 17 #error "Never include this file directly; instead, include <fcntl.h>" 18 #endif 19 20 #include "fortify.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 int __open_chk(const char*, int); 27 int __openat_chk(int, const char*, int); 28 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) 29 int __open64_chk(const char*, int); 30 int __openat64_chk(int, const char*, int); 31 #endif 32 /* 33 * Even in musl FORTIFY, the following is the easiest way to call a real open. 34 */ 35 int __open_real(const char*, int, ...) __DIAGNOSE_RENAME(open); 36 int __openat_real(int, const char*, int, ...) __DIAGNOSE_RENAME(openat); 37 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) 38 int __open64_real(const char*, int, ...) __DIAGNOSE_RENAME(open64); 39 int __openat64_real(int, const char*, int, ...) __DIAGNOSE_RENAME(openat64); 40 #endif 41 42 #ifdef __FORTIFY_COMPILATION 43 __DIAGNOSE_FORTIFY_INLINE 44 int open(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags) 45 __DIAGNOSE_OVERLOAD 46 __DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'open' " OPEN_TOO_FEW_ARGS_ERROR) 47 { 48 #ifdef __FORTIFY_RUNTIME 49 return __open_chk(path, flags); 50 #else 51 return __open_real(path, flags); 52 #endif 53 } 54 55 __DIAGNOSE_FORTIFY_INLINE 56 int open(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, unsigned modes) 57 __DIAGNOSE_OVERLOAD 58 __DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'open' " OPEN_USELESS_MODES_WARNING) 59 { 60 return __open_real(path, flags, modes); 61 } 62 63 __DIAGNOSE_FORTIFY_INLINE 64 int openat(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags) 65 __DIAGNOSE_OVERLOAD 66 __DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'openat' " OPEN_TOO_FEW_ARGS_ERROR) 67 { 68 #ifdef __FORTIFY_RUNTIME 69 return __openat_chk(dirfd, path, flags); 70 #else 71 return __openat_real(dirfd, path, flags); 72 #endif 73 } 74 75 __DIAGNOSE_FORTIFY_INLINE 76 int openat(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes) 77 __DIAGNOSE_OVERLOAD 78 __DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'openat' " OPEN_USELESS_MODES_WARNING) 79 { 80 return __openat_real(dirfd, path, flags, modes); 81 } 82 83 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) 84 __DIAGNOSE_FORTIFY_INLINE 85 int open64(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags) 86 __DIAGNOSE_OVERLOAD 87 __DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'open64' " OPEN_TOO_FEW_ARGS_ERROR) 88 { 89 #ifdef __FORTIFY_RUNTIME 90 return __open64_chk(path, flags); 91 #else 92 return __open64_real(path, flags); 93 #endif 94 } 95 96 __DIAGNOSE_FORTIFY_INLINE 97 int open64(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes) 98 __DIAGNOSE_OVERLOAD 99 __DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'open64' " OPEN_USELESS_MODES_WARNING) 100 { 101 return __open64_real(path, flags, modes); 102 } 103 104 __DIAGNOSE_FORTIFY_INLINE 105 int openat64(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags) 106 __DIAGNOSE_OVERLOAD 107 __DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'openat64' " OPEN_TOO_FEW_ARGS_ERROR) 108 { 109 #ifdef __FORTIFY_RUNTIME 110 return __openat64_chk(dirfd, path, flags); 111 #else 112 return __openat64_real(dirfd, path, flags); 113 #endif 114 } 115 116 __DIAGNOSE_FORTIFY_INLINE 117 int openat64(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes) 118 __DIAGNOSE_OVERLOAD 119 __DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'openat64' " OPEN_USELESS_MODES_WARNING) 120 { 121 return __openat64_real(dirfd, path, flags, modes); 122 } 123 #endif 124 125 #endif 126 127 #ifdef __cplusplus 128 } 129 #endif