static prop_area* map_prop_area(const char* filename, bool is_legacy) { int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); bool close_fd = true; if (fd == -1 && errno == ENOENT && is_legacy) { /* * For backwards compatibility, if the file doesn't * exist, we use the environment to get the file descriptor. * For security reasons, we only use this backup if the kernel * returns ENOENT. We don't want to use the backup if the kernel * returns other errors such as ENOMEM or ENFILE, since it * might be possible for an external program to trigger this * condition. * Only do this for the legacy prop file, secured prop files * do not have a backup */ fd = get_fd_from_env(); close_fd = false; } if (fd < 0) { return nullptr; } prop_area* map_result = map_fd_ro(fd); if (close_fd) { close(fd); } return map_result; }
static int map_prop_area() { int fd = open(property_filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); bool close_fd = true; if (fd == -1 && errno == ENOENT) { /* * For backwards compatibility, if the file doesn't * exist, we use the environment to get the file descriptor. * For security reasons, we only use this backup if the kernel * returns ENOENT. We don't want to use the backup if the kernel * returns other errors such as ENOMEM or ENFILE, since it * might be possible for an external program to trigger this * condition. */ fd = get_fd_from_env(); close_fd = false; } if (fd < 0) { return -1; } const int map_result = map_fd_ro(fd); if (close_fd) { close(fd); } return map_result; }
static prop_area* map_prop_area(const char* filename #if MB_ENABLE_COMPAT_PROPERTIES , bool is_legacy #endif ) { int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | #if MB_ALLOW_DIRECT_CLIENT_WRITES O_RDWR #else O_RDONLY #endif ); #if MB_ENABLE_COMPAT_PROPERTIES bool close_fd = true; if (fd == -1 && errno == ENOENT && is_legacy) { /* * For backwards compatibility, if the file doesn't * exist, we use the environment to get the file descriptor. * For security reasons, we only use this backup if the kernel * returns ENOENT. We don't want to use the backup if the kernel * returns other errors such as ENOMEM or ENFILE, since it * might be possible for an external program to trigger this * condition. * Only do this for the legacy prop file, secured prop files * do not have a backup */ fd = get_fd_from_env(); close_fd = false; } #endif if (fd == -1) return nullptr; prop_area* map_result = map_fd_ro(fd); #if MB_ENABLE_COMPAT_PROPERTIES if (close_fd) { #endif close(fd); #if MB_ENABLE_COMPAT_PROPERTIES } #endif return map_result; }