Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #pragma once |
| 30 | |
Elliott Hughes | 414dd2d | 2024-10-16 14:48:30 +0000 | [diff] [blame] | 31 | #include <sys/cdefs.h> |
| 32 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 33 | #include <stdbool.h> |
| 34 | #include <stdint.h> |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 35 | |
| 36 | __BEGIN_DECLS |
| 37 | |
| 38 | /* |
| 39 | * Error checking for close(2). |
| 40 | * |
| 41 | * Mishandling of file descriptor ownership is a common source of errors that |
| 42 | * can be extremely difficult to diagnose. Mistakes like the following can |
| 43 | * result in seemingly 'impossible' failures showing up on other threads that |
| 44 | * happened to try to open a file descriptor between the buggy code's close and |
| 45 | * fclose: |
| 46 | * |
| 47 | * int print(int fd) { |
| 48 | * int rc; |
| 49 | * char buf[128]; |
| 50 | * while ((rc = read(fd, buf, sizeof(buf))) > 0) { |
| 51 | * printf("%.*s", rc); |
| 52 | * } |
| 53 | * close(fd); |
| 54 | * } |
| 55 | * |
| 56 | * int bug() { |
| 57 | * FILE* f = fopen("foo", "r"); |
| 58 | * print(fileno(f)); |
| 59 | * fclose(f); |
| 60 | * } |
| 61 | * |
| 62 | * To make it easier to find this class of bugs, bionic provides a method to |
| 63 | * require that file descriptors are closed by their owners. File descriptors |
| 64 | * can be associated with tags with which they must be closed. This allows |
| 65 | * objects that conceptually own an fd (FILE*, unique_fd, etc.) to use their |
| 66 | * own address at the tag, to enforce that closure of the fd must come as a |
| 67 | * result of their own destruction (fclose, ~unique_fd, etc.) |
| 68 | * |
| 69 | * By default, a file descriptor's tag is 0, and close(fd) is equivalent to |
| 70 | * closing fd with the tag 0. |
| 71 | */ |
| 72 | |
| 73 | /* |
| 74 | * For improved diagnostics, the type of a file descriptors owner can be |
| 75 | * encoded in the most significant byte of the owner tag. Values of 0 and 0xff |
| 76 | * are ignored, which allows for raw pointers to be used as owner tags without |
| 77 | * modification. |
| 78 | */ |
| 79 | enum android_fdsan_owner_type { |
| 80 | /* |
| 81 | * Generic Java or native owners. |
| 82 | * |
| 83 | * Generic Java objects always use 255 as their type, using identityHashCode |
| 84 | * as the value of the tag, leaving bits 33-56 unset. Native pointers are sign |
| 85 | * extended from 48-bits of virtual address space, and so can have the MSB |
| 86 | * set to 255 as well. Use the value of bits 49-56 to distinguish between |
| 87 | * these cases. |
| 88 | */ |
| 89 | ANDROID_FDSAN_OWNER_TYPE_GENERIC_00 = 0, |
| 90 | ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF = 255, |
| 91 | |
| 92 | /* FILE* */ |
| 93 | ANDROID_FDSAN_OWNER_TYPE_FILE = 1, |
| 94 | |
| 95 | /* DIR* */ |
| 96 | ANDROID_FDSAN_OWNER_TYPE_DIR = 2, |
| 97 | |
| 98 | /* android::base::unique_fd */ |
| 99 | ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD = 3, |
| 100 | |
Josh Gao | 27cc4bc | 2018-08-22 12:40:08 -0700 | [diff] [blame] | 101 | /* sqlite-owned file descriptors */ |
| 102 | ANDROID_FDSAN_OWNER_TYPE_SQLITE = 4, |
| 103 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 104 | /* java.io.FileInputStream */ |
Josh Gao | c1b4476 | 2018-08-23 14:21:03 -0700 | [diff] [blame] | 105 | ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM = 5, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 106 | |
| 107 | /* java.io.FileOutputStream */ |
Josh Gao | c1b4476 | 2018-08-23 14:21:03 -0700 | [diff] [blame] | 108 | ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM = 6, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 109 | |
| 110 | /* java.io.RandomAccessFile */ |
Josh Gao | c1b4476 | 2018-08-23 14:21:03 -0700 | [diff] [blame] | 111 | ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE = 7, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 112 | |
| 113 | /* android.os.ParcelFileDescriptor */ |
Josh Gao | c1b4476 | 2018-08-23 14:21:03 -0700 | [diff] [blame] | 114 | ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR = 8, |
Josh Gao | ab6a080 | 2018-08-30 17:37:52 -0700 | [diff] [blame] | 115 | |
| 116 | /* ART FdFile */ |
| 117 | ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE = 9, |
Josh Gao | d527cf1 | 2018-09-07 12:46:02 -0700 | [diff] [blame] | 118 | |
| 119 | /* java.net.DatagramSocketImpl */ |
| 120 | ANDROID_FDSAN_OWNER_TYPE_DATAGRAMSOCKETIMPL = 10, |
| 121 | |
| 122 | /* java.net.SocketImpl */ |
| 123 | ANDROID_FDSAN_OWNER_TYPE_SOCKETIMPL = 11, |
Josh Gao | 5fa9df8 | 2018-09-07 12:47:00 -0700 | [diff] [blame] | 124 | |
| 125 | /* libziparchive's ZipArchive */ |
| 126 | ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE = 12, |
Brian Duddie | 5cfca6b | 2022-08-24 19:51:20 +0000 | [diff] [blame] | 127 | |
| 128 | /* native_handle_t */ |
| 129 | ANDROID_FDSAN_OWNER_TYPE_NATIVE_HANDLE = 13, |
Steven Moreland | 1d413fc | 2023-06-22 23:03:57 +0000 | [diff] [blame] | 130 | |
| 131 | /* android::Parcel */ |
| 132 | ANDROID_FDSAN_OWNER_TYPE_PARCEL = 14, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * Create an owner tag with the specified type and least significant 56 bits of tag. |
| 137 | */ |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 138 | |
| 139 | #if __BIONIC_AVAILABILITY_GUARD(29) |
Elliott Hughes | 78e9ebc | 2019-03-14 09:29:52 -0700 | [diff] [blame] | 140 | uint64_t android_fdsan_create_owner_tag(enum android_fdsan_owner_type type, uint64_t tag) __INTRODUCED_IN(29) __attribute__((__weak__)); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Exchange a file descriptor's tag. |
| 144 | * |
| 145 | * Logs and aborts if the fd's tag does not match expected_tag. |
| 146 | */ |
Elliott Hughes | 78e9ebc | 2019-03-14 09:29:52 -0700 | [diff] [blame] | 147 | void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) __INTRODUCED_IN(29) __attribute__((__weak__)); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Close a file descriptor with a tag, and resets the tag to 0. |
| 151 | * |
| 152 | * Logs and aborts if the tag is incorrect. |
| 153 | */ |
Elliott Hughes | 78e9ebc | 2019-03-14 09:29:52 -0700 | [diff] [blame] | 154 | int android_fdsan_close_with_tag(int fd, uint64_t tag) __INTRODUCED_IN(29) __attribute__((__weak__)); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 155 | |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 156 | /* |
| 157 | * Get a file descriptor's current owner tag. |
| 158 | * |
| 159 | * Returns 0 for untagged and invalid file descriptors. |
| 160 | */ |
Logan Chien | c9e7018 | 2019-03-20 22:00:55 +0800 | [diff] [blame] | 161 | uint64_t android_fdsan_get_owner_tag(int fd) __INTRODUCED_IN(29); |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Get an owner tag's string representation. |
| 165 | * |
| 166 | * The return value points to memory with static lifetime, do not attempt to modify it. |
| 167 | */ |
zijunzhao | ac6d59c | 2023-04-12 23:26:46 +0000 | [diff] [blame] | 168 | const char* _Nonnull android_fdsan_get_tag_type(uint64_t tag) __INTRODUCED_IN(29); |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Get an owner tag's value, with the type masked off. |
| 172 | */ |
Logan Chien | c9e7018 | 2019-03-20 22:00:55 +0800 | [diff] [blame] | 173 | uint64_t android_fdsan_get_tag_value(uint64_t tag) __INTRODUCED_IN(29); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 174 | #endif /* __BIONIC_AVAILABILITY_GUARD(29) */ |
| 175 | |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 176 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 177 | enum android_fdsan_error_level { |
| 178 | // No errors. |
| 179 | ANDROID_FDSAN_ERROR_LEVEL_DISABLED, |
| 180 | |
| 181 | // Warn once(ish) on error, and then downgrade to ANDROID_FDSAN_ERROR_LEVEL_DISABLED. |
| 182 | ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE, |
| 183 | |
| 184 | // Warn always on error. |
| 185 | ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS, |
| 186 | |
| 187 | // Abort on error. |
| 188 | ANDROID_FDSAN_ERROR_LEVEL_FATAL, |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * Get the error level. |
| 193 | */ |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 194 | |
| 195 | #if __BIONIC_AVAILABILITY_GUARD(29) |
Elliott Hughes | 78e9ebc | 2019-03-14 09:29:52 -0700 | [diff] [blame] | 196 | enum android_fdsan_error_level android_fdsan_get_error_level() __INTRODUCED_IN(29) __attribute__((__weak__)); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Set the error level and return the previous state. |
| 200 | * |
| 201 | * Error checking is automatically disabled in the child of a fork, to maintain |
Elliott Hughes | cf34653 | 2020-07-31 10:35:03 -0700 | [diff] [blame] | 202 | * compatibility with code that forks, closes all file descriptors, and then |
| 203 | * execs. |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 204 | * |
| 205 | * In cases such as the zygote, where the child has no intention of calling |
| 206 | * exec, call this function to reenable fdsan checks. |
| 207 | * |
| 208 | * This function is not thread-safe and does not synchronize with checks of the |
| 209 | * value, and so should probably only be called in single-threaded contexts |
| 210 | * (e.g. postfork). |
| 211 | */ |
Elliott Hughes | 78e9ebc | 2019-03-14 09:29:52 -0700 | [diff] [blame] | 212 | enum android_fdsan_error_level android_fdsan_set_error_level(enum android_fdsan_error_level new_level) __INTRODUCED_IN(29) __attribute__((__weak__)); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 213 | #endif /* __BIONIC_AVAILABILITY_GUARD(29) */ |
| 214 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 215 | |
Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Set the error level to the global setting if available, or a default value. |
| 218 | */ |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 219 | |
| 220 | #if __BIONIC_AVAILABILITY_GUARD(30) |
Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 221 | enum android_fdsan_error_level android_fdsan_set_error_level_from_property(enum android_fdsan_error_level default_level) __INTRODUCED_IN(30) __attribute__((__weak__)); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 222 | #endif /* __BIONIC_AVAILABILITY_GUARD(30) */ |
| 223 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 224 | __END_DECLS |