OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/signature_evaluator_mac.h" |
| 6 |
| 7 #include <CoreFoundation/CoreFoundation.h> |
| 8 #include <Foundation/Foundation.h> |
| 9 #include <Security/Security.h> |
| 10 #include <sys/xattr.h> |
| 11 |
| 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/mac/mac_util.h" |
| 14 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/mac/scoped_nsobject.h" |
| 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "chrome/common/safe_browsing/binary_feature_extractor.h" |
| 18 #include "chrome/common/safe_browsing/csd.pb.h" |
| 19 #include "chrome/common/safe_browsing/mach_o_image_reader_mac.h" |
| 20 |
| 21 namespace safe_browsing { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // OS X code signing data can be stored in extended attributes as well. This is |
| 26 // a list of the extended attributes slots currently used in Security.framework, |
| 27 // from codesign.h (see the kSecCS_* constants). |
| 28 const char* const xattrs[] = { |
| 29 "com.apple.cs.CodeDirectory", |
| 30 "com.apple.cs.CodeSignature", |
| 31 "com.apple.cs.CodeRequirements", |
| 32 "com.apple.cs.CodeResources", |
| 33 "com.apple.cs.CodeApplication", |
| 34 "com.apple.cs.CodeEntitlements", |
| 35 }; |
| 36 |
| 37 // Convenience function to get the appropriate path from a variety of NSObject |
| 38 // types. For resources, code signing seems to give back an NSURL in which |
| 39 // the path is relative to the bundle root. So in this case, we take the |
| 40 // relative component, otherwise we take the entire path. |
| 41 bool GetPathFromNSObject(id obj, std::string* output) { |
| 42 if (NSString* str = base::mac::ObjCCast<NSString>(obj)) { |
| 43 output->assign([str fileSystemRepresentation]); |
| 44 return true; |
| 45 } |
| 46 if (NSURL* url = base::mac::ObjCCast<NSURL>(obj)) { |
| 47 output->assign([[url path] fileSystemRepresentation]); |
| 48 return true; |
| 49 } |
| 50 if (NSBundle* bundle = base::mac::ObjCCast<NSBundle>(obj)) { |
| 51 output->assign([[bundle bundlePath] fileSystemRepresentation]); |
| 52 return true; |
| 53 } |
| 54 return false; |
| 55 } |
| 56 |
| 57 // Extract the signature information from the mach-o or extended attributes. |
| 58 void ExtractSignatureInfo(const base::FilePath& path, |
| 59 ClientDownloadRequest_ImageHeaders* image_headers, |
| 60 ClientDownloadRequest_SignatureInfo* signature) { |
| 61 scoped_refptr<BinaryFeatureExtractor> bfe = new BinaryFeatureExtractor(); |
| 62 |
| 63 // TODO(kerrnel): if Chrome ever opts into the OS X "kill" semantics, this |
| 64 // call has to change. `ExtractImageFeatures` maps the file, which will |
| 65 // cause Chrome to be killed before it can report on the invalid file. |
| 66 // This call will need to read(2) the binary into a buffer. |
| 67 if (!bfe->ExtractImageFeatures(path, BinaryFeatureExtractor::kDefaultOptions, |
| 68 image_headers, |
| 69 signature->mutable_signed_data())) { |
| 70 // If this is not a mach-o file, search inside the extended attributes. |
| 71 for (const char* attr : xattrs) { |
| 72 ssize_t size = getxattr(path.value().c_str(), attr, nullptr, 0, 0, 0); |
| 73 if (size >= 0) { |
| 74 std::vector<uint8_t> xattr_data(size); |
| 75 ssize_t post_size = getxattr(path.value().c_str(), attr, &xattr_data[0], |
| 76 xattr_data.size(), 0, 0); |
| 77 if (post_size >= 0) { |
| 78 xattr_data.resize(post_size); |
| 79 ClientDownloadRequest_ExtendedAttr* xattr_msg = |
| 80 signature->add_xattr(); |
| 81 xattr_msg->set_key(attr); |
| 82 xattr_msg->set_value(xattr_data.data(), xattr_data.size()); |
| 83 } |
| 84 } |
| 85 } |
| 86 } |
| 87 } |
| 88 |
| 89 // Process the NSError information about any files that were altered. |
| 90 void ReportAlteredFiles( |
| 91 id detail, |
| 92 const base::FilePath& bundle_path, |
| 93 ClientIncidentReport_IncidentData_BinaryIntegrityIncident* incident) { |
| 94 if (NSArray* arr = base::mac::ObjCCast<NSArray>(detail)) { |
| 95 for (id obj in arr) |
| 96 ReportAlteredFiles(obj, bundle_path, incident); |
| 97 } else { |
| 98 std::string path_str; |
| 99 if (!GetPathFromNSObject(detail, &path_str)) |
| 100 return; |
| 101 std::string relative_path; |
| 102 base::FilePath path(path_str); |
| 103 // If the relative path calculation fails, at least take the basename. |
| 104 if (!MacSignatureEvaluator::GetRelativePathComponent(bundle_path, path, |
| 105 &relative_path)) { |
| 106 relative_path = path.BaseName().value(); |
| 107 } |
| 108 |
| 109 ClientIncidentReport_IncidentData_BinaryIntegrityIncident_ContainedFile* |
| 110 contained_file = incident->add_contained_file(); |
| 111 contained_file->set_relative_path(relative_path); |
| 112 ExtractSignatureInfo(base::FilePath(path_str), |
| 113 contained_file->mutable_image_headers(), |
| 114 contained_file->mutable_signature()); |
| 115 } |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 MacSignatureEvaluator::MacSignatureEvaluator( |
| 121 const base::FilePath& signed_object_path) |
| 122 : path_(signed_object_path), |
| 123 requirement_str_(), |
| 124 has_requirement_(false), |
| 125 code_(nullptr), |
| 126 requirement_(nullptr) {} |
| 127 |
| 128 MacSignatureEvaluator::MacSignatureEvaluator( |
| 129 const base::FilePath& signed_object_path, |
| 130 const std::string& requirement) |
| 131 : path_(signed_object_path), |
| 132 requirement_str_(requirement), |
| 133 has_requirement_(true), |
| 134 code_(nullptr), |
| 135 requirement_(nullptr) {} |
| 136 |
| 137 MacSignatureEvaluator::~MacSignatureEvaluator() {} |
| 138 |
| 139 bool MacSignatureEvaluator::GetRelativePathComponent( |
| 140 const base::FilePath& parent, |
| 141 const base::FilePath& child, |
| 142 std::string* out) { |
| 143 if (!parent.IsParent(child)) |
| 144 return false; |
| 145 |
| 146 std::vector<base::FilePath::StringType> parent_components; |
| 147 std::vector<base::FilePath::StringType> child_components; |
| 148 parent.GetComponents(&parent_components); |
| 149 child.GetComponents(&child_components); |
| 150 |
| 151 size_t i = 0; |
| 152 while (i < parent_components.size() && |
| 153 child_components[i] == parent_components[i]) { |
| 154 ++i; |
| 155 } |
| 156 |
| 157 while (i < child_components.size()) { |
| 158 out->append(child_components[i]); |
| 159 if (++i < child_components.size()) |
| 160 out->append("/"); |
| 161 } |
| 162 return true; |
| 163 } |
| 164 |
| 165 bool MacSignatureEvaluator::Initialize() { |
| 166 base::scoped_nsobject<NSURL> code_url([[NSURL alloc] |
| 167 initFileURLWithPath:base::SysUTF8ToNSString(path_.value())]); |
| 168 if (!code_url) |
| 169 return false; |
| 170 |
| 171 if (SecStaticCodeCreateWithPath(base::mac::NSToCFCast(code_url.get()), |
| 172 kSecCSDefaultFlags, |
| 173 code_.InitializeInto()) != errSecSuccess) { |
| 174 return false; |
| 175 } |
| 176 |
| 177 if (has_requirement_) { |
| 178 if (SecRequirementCreateWithString( |
| 179 base::mac::NSToCFCast(base::SysUTF8ToNSString(requirement_str_)), |
| 180 kSecCSDefaultFlags, |
| 181 requirement_.InitializeInto()) != errSecSuccess) { |
| 182 return false; |
| 183 } |
| 184 } |
| 185 return true; |
| 186 } |
| 187 |
| 188 bool MacSignatureEvaluator::PerformEvaluation( |
| 189 ClientIncidentReport_IncidentData_BinaryIntegrityIncident* incident) { |
| 190 DCHECK(incident->contained_file_size() == 0); |
| 191 base::ScopedCFTypeRef<CFErrorRef> errors; |
| 192 OSStatus err = SecStaticCodeCheckValidityWithErrors( |
| 193 code_, kSecCSCheckAllArchitectures, requirement_, |
| 194 errors.InitializeInto()); |
| 195 if (err == errSecSuccess) |
| 196 return true; |
| 197 // Add the signature of the main binary to the incident report. |
| 198 incident->set_file_basename(path_.BaseName().value()); |
| 199 incident->set_sec_error(err); |
| 200 // We heuristically detect if we are in a bundle or not by checking if |
| 201 // the main executable is different from the path_. |
| 202 base::ScopedCFTypeRef<CFDictionaryRef> info_dict; |
| 203 if (SecCodeCopySigningInformation(code_, kSecCSDefaultFlags, |
| 204 info_dict.InitializeInto()) == |
| 205 errSecSuccess) { |
| 206 CFURLRef exec_url = base::mac::CFCastStrict<CFURLRef>( |
| 207 CFDictionaryGetValue(info_dict, kSecCodeInfoMainExecutable)); |
| 208 if (!exec_url) |
| 209 return false; |
| 210 |
| 211 base::FilePath exec_path( |
| 212 [[base::mac::CFToNSCast(exec_url) path] fileSystemRepresentation]); |
| 213 if (exec_path != path_) { |
| 214 ReportAlteredFiles(base::mac::CFToNSCast(exec_url), path_, incident); |
| 215 } else { |
| 216 // We may be examing a flat executable file, so extract any signature. |
| 217 ExtractSignatureInfo(path_, incident->mutable_image_headers(), |
| 218 incident->mutable_signature()); |
| 219 } |
| 220 } |
| 221 |
| 222 if (errors) { |
| 223 NSDictionary* info = [base::mac::CFToNSCast(errors.get()) userInfo]; |
| 224 static const CFStringRef keys[] = { |
| 225 kSecCFErrorResourceAltered, kSecCFErrorResourceMissing, |
| 226 }; |
| 227 for (CFStringRef key : keys) { |
| 228 if (id detail = [info objectForKey:base::mac::CFToNSCast(key)]) |
| 229 ReportAlteredFiles(detail, path_, incident); |
| 230 } |
| 231 } |
| 232 return false; |
| 233 } |
| 234 |
| 235 } // namespace safe_browsing |
OLD | NEW |