Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 10 | #include "base/ios/device_util.h" |
| 11 | |
| 12 | #include <CommonCrypto/CommonDigest.h> |
[email protected] | aebcd0dd | 2012-10-05 17:48:58 | [diff] [blame] | 13 | #import <UIKit/UIKit.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 14 | #include <ifaddrs.h> |
| 15 | #include <net/if_dl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include <stddef.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/sysctl.h> |
| 20 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
Avi Drissman | a09d7dd | 2023-08-17 16:26:58 | [diff] [blame] | 23 | #include "base/apple/scoped_cftyperef.h" |
Hans Wennborg | a47ddf8 | 2020-05-05 18:08:07 | [diff] [blame] | 24 | #include "base/check.h" |
Peter Kasting | 2f61c8b | 2022-07-19 23:43:46 | [diff] [blame] | 25 | #include "base/numerics/safe_conversions.h" |
Avi Drissman | 837d106b | 2023-09-12 14:51:20 | [diff] [blame] | 26 | #include "base/posix/sysctl.h" |
[email protected] | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 27 | #include "base/strings/stringprintf.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 28 | #include "base/strings/sys_string_conversions.h" |
Asami Doi | 5fabc525 | 2024-03-20 11:11:46 | [diff] [blame] | 29 | #include "base/system/sys_info.h" |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // Client ID key in the user preferences. |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 34 | NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID"; |
| 35 | NSString* const kClientIdPreferenceKey = @"ChromeClientID"; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 36 | // Current hardware type. This is used to detect that a device has been backed |
| 37 | // up and restored to another device, and allows regenerating a new device id. |
| 38 | NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType"; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 39 | // Default salt for device ids. |
| 40 | const char kDefaultSalt[] = "Salt"; |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 41 | // Zero UUID returned on buggy iOS devices. |
| 42 | NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000"; |
| 43 | |
| 44 | NSString* GenerateClientId() { |
| 45 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 46 | |
| 47 | // Try to migrate from legacy client id. |
| 48 | NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; |
| 49 | |
| 50 | // Some iOS6 devices return a buggy identifierForVendor: |
Avi Drissman | b34db7a | 2023-01-04 21:50:15 | [diff] [blame] | 51 | // https://openradar.appspot.com/12377282. If this is the case, revert to |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 52 | // generating a new one. |
| 53 | if (!client_id || [client_id isEqualToString:kZeroUUID]) { |
[email protected] | d01f311 | 2014-06-19 22:49:55 | [diff] [blame] | 54 | client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 55 | if ([client_id isEqualToString:kZeroUUID]) { |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 56 | client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 57 | } |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 58 | } |
| 59 | return client_id; |
| 60 | } |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 61 | |
| 62 | } // namespace |
| 63 | |
Avi Drissman | 354864c | 2023-06-01 20:06:03 | [diff] [blame] | 64 | namespace ios::device_util { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 65 | |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 66 | bool RamIsAtLeast512Mb() { |
| 67 | // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe. |
| 68 | return RamIsAtLeast(450); |
| 69 | } |
| 70 | |
| 71 | bool RamIsAtLeast1024Mb() { |
| 72 | // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe. |
| 73 | return RamIsAtLeast(900); |
| 74 | } |
| 75 | |
| 76 | bool RamIsAtLeast(uint64_t ram_in_mb) { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 77 | uint64_t memory_size = 0; |
| 78 | size_t size = sizeof(memory_size); |
| 79 | if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) { |
[email protected] | 9317c249 | 2013-12-11 22:18:41 | [diff] [blame] | 80 | // Anything >= 500M, call high ram. |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 81 | return memory_size >= ram_in_mb * 1024 * 1024; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | bool IsSingleCoreDevice() { |
| 87 | uint64_t cpu_number = 0; |
| 88 | size_t sizes = sizeof(cpu_number); |
| 89 | sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0); |
| 90 | return cpu_number == 1; |
| 91 | } |
| 92 | |
| 93 | std::string GetMacAddress(const std::string& interface_name) { |
| 94 | std::string mac_string; |
| 95 | struct ifaddrs* addresses; |
| 96 | if (getifaddrs(&addresses) == 0) { |
| 97 | for (struct ifaddrs* address = addresses; address; |
| 98 | address = address->ifa_next) { |
| 99 | if ((address->ifa_addr->sa_family == AF_LINK) && |
| 100 | strcmp(interface_name.c_str(), address->ifa_name) == 0) { |
| 101 | const struct sockaddr_dl* found_address_struct = |
| 102 | reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr); |
| 103 | |
| 104 | // |found_address_struct->sdl_data| contains the interface name followed |
| 105 | // by the interface address. The address part can be accessed based on |
| 106 | // the length of the name, that is, |found_address_struct->sdl_nlen|. |
| 107 | const unsigned char* found_address = |
| 108 | reinterpret_cast<const unsigned char*>( |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 109 | &found_address_struct |
| 110 | ->sdl_data[found_address_struct->sdl_nlen]); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 111 | |
| 112 | int found_address_length = found_address_struct->sdl_alen; |
| 113 | for (int i = 0; i < found_address_length; ++i) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 114 | if (i != 0) { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 115 | mac_string.push_back(':'); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 116 | } |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 117 | base::StringAppendF(&mac_string, "%02X", found_address[i]); |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | freeifaddrs(addresses); |
| 123 | } |
| 124 | return mac_string; |
| 125 | } |
| 126 | |
| 127 | std::string GetRandomId() { |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 128 | base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 129 | CFUUIDCreate(kCFAllocatorDefault)); |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 130 | base::apple::ScopedCFTypeRef<CFStringRef> uuid_string( |
Avi Drissman | 2406f2a | 2023-11-06 14:32:40 | [diff] [blame] | 131 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get())); |
| 132 | return base::SysCFStringRefToUTF8(uuid_string.get()); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | std::string GetDeviceIdentifier(const char* salt) { |
| 136 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 137 | |
| 138 | NSString* last_seen_hardware = |
| 139 | [defaults stringForKey:kHardwareTypePreferenceKey]; |
Asami Doi | 5fabc525 | 2024-03-20 11:11:46 | [diff] [blame] | 140 | NSString* current_hardware = |
| 141 | base::SysUTF8ToNSString(base::SysInfo::HardwareModelName()); |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 142 | if (!last_seen_hardware) { |
| 143 | last_seen_hardware = current_hardware; |
| 144 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
| 145 | [defaults synchronize]; |
| 146 | } |
| 147 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 148 | NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
| 149 | |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 150 | if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 151 | client_id = GenerateClientId(); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 152 | [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 153 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 154 | [defaults synchronize]; |
| 155 | } |
| 156 | |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 157 | return GetSaltedString(base::SysNSStringToUTF8(client_id), |
| 158 | salt ? salt : kDefaultSalt); |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 159 | } |
| 160 | |
Guillaume Jenkins | 74049b1 | 2020-12-01 18:53:46 | [diff] [blame] | 161 | std::string GetVendorId() { |
| 162 | return base::SysNSStringToUTF8( |
| 163 | [[[UIDevice currentDevice] identifierForVendor] UUIDString]); |
| 164 | } |
| 165 | |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 166 | std::string GetSaltedString(const std::string& in_string, |
| 167 | const std::string& salt) { |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 168 | DCHECK(salt.length()); |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 169 | NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt) |
| 170 | dataUsingEncoding:NSUTF8StringEncoding]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 171 | |
| 172 | unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
Peter Kasting | 2f61c8b | 2022-07-19 23:43:46 | [diff] [blame] | 173 | CC_SHA256([hash_data bytes], base::checked_cast<CC_LONG>([hash_data length]), |
| 174 | hash); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 175 | CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); |
| 176 | |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 177 | base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 178 | CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 179 | base::apple::ScopedCFTypeRef<CFStringRef> device_id( |
Avi Drissman | 2406f2a | 2023-11-06 14:32:40 | [diff] [blame] | 180 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get())); |
| 181 | return base::SysCFStringRefToUTF8(device_id.get()); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 182 | } |
| 183 | |
Alexandra Pereira | a675499b | 2025-04-03 16:52:50 | [diff] [blame] | 184 | base::expected<task_vm_info, kern_return_t> GetTaskVMInfo() { |
| 185 | task_vm_info task_vm_info_data = {0}; |
| 186 | mach_msg_type_number_t count = TASK_VM_INFO_COUNT; |
| 187 | kern_return_t result = |
| 188 | task_info(mach_task_self(), TASK_VM_INFO, |
| 189 | reinterpret_cast<task_info_t>(&task_vm_info_data), &count); |
| 190 | if (result == KERN_SUCCESS) { |
| 191 | return task_vm_info_data; |
| 192 | } |
| 193 | return base::unexpected(result); |
| 194 | } |
Avi Drissman | 354864c | 2023-06-01 20:06:03 | [diff] [blame] | 195 | } // namespace ios::device_util |