1 /* 2 * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/cdsProtectionDomain.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/classLoaderExt.hpp" 31 #include "classfile/javaClasses.hpp" 32 #include "classfile/moduleEntry.hpp" 33 #include "classfile/systemDictionaryShared.hpp" 34 #include "classfile/vmClasses.hpp" 35 #include "classfile/vmSymbols.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "memory/universe.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/symbol.hpp" 41 #include "runtime/javaCalls.hpp" 42 43 OopHandle CDSProtectionDomain::_shared_protection_domains; 44 OopHandle CDSProtectionDomain::_shared_jar_urls; 45 OopHandle CDSProtectionDomain::_shared_jar_manifests; 46 47 // Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with 48 // the given InstanceKlass. 49 // Returns the ProtectionDomain for the InstanceKlass. 50 Handle CDSProtectionDomain::init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS) { 51 int index = ik->shared_classpath_index(); 52 assert(index >= 0, "Sanity"); 53 SharedClassPathEntry* ent = FileMapInfo::shared_path(index); 54 Symbol* class_name = ik->name(); 55 56 if (ent->is_modules_image()) { 57 // For shared app/platform classes originated from the run-time image: 58 // The ProtectionDomains are cached in the corresponding ModuleEntries 59 // for fast access by the VM. 60 // all packages from module image are already created during VM bootstrap in 61 // Modules::define_module(). 62 assert(pkg_entry != nullptr, "archived class in module image cannot be from unnamed package"); 63 Handle archived_pd = get_archived_protection_domain(THREAD, ik); 64 if (archived_pd.not_null()) { 65 return archived_pd; 66 } 67 ModuleEntry* mod_entry = pkg_entry->module(); 68 return get_shared_protection_domain(class_loader, mod_entry, THREAD); 69 } else { 70 // For shared app/platform classes originated from JAR files on the class path: 71 // Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length 72 // as the shared classpath table in the shared archive (see 73 // FileMap::_shared_path_table in filemap.hpp for details). 74 // 75 // If a shared InstanceKlass k is loaded from the class path, let 76 // 77 // index = k->shared_classpath_index(): 78 // 79 // FileMap::_shared_path_table[index] identifies the JAR file that contains k. 80 // 81 // k's protection domain is: 82 // 83 // ProtectionDomain pd = _shared_protection_domains[index]; 84 // 85 // and k's Package is initialized using 86 // 87 // manifest = _shared_jar_manifests[index]; 88 // url = _shared_jar_urls[index]; 89 // define_shared_package(class_name, class_loader, manifest, url, CHECK_NH); 90 // 91 // Note that if an element of these 3 _shared_xxx arrays is null, it will be initialized by 92 // the corresponding SystemDictionaryShared::get_shared_xxx() function. 93 Handle manifest = get_shared_jar_manifest(index, CHECK_NH); 94 Handle url = get_shared_jar_url(index, CHECK_NH); 95 if (!CDSConfig::is_loading_packages()) { 96 int index_offset = index - ClassLoaderExt::app_class_paths_start_index(); 97 if (index_offset < PackageEntry::max_index_for_defined_in_class_path()) { 98 if (pkg_entry == nullptr || !pkg_entry->is_defined_by_cds_in_class_path(index_offset)) { 99 // define_shared_package only needs to be called once for each package in a jar specified 100 // in the shared class path. 101 define_shared_package(class_name, class_loader, manifest, url, CHECK_NH); 102 if (pkg_entry != nullptr) { 103 pkg_entry->set_defined_by_cds_in_class_path(index_offset); 104 } 105 } 106 } else { 107 define_shared_package(class_name, class_loader, manifest, url, CHECK_NH); 108 } 109 } 110 Handle archived_pd = get_archived_protection_domain(THREAD, ik); 111 if (archived_pd.not_null()) { 112 return archived_pd; 113 } 114 return get_shared_protection_domain(class_loader, index, url, THREAD); 115 } 116 } 117 118 Handle CDSProtectionDomain::get_package_name(Symbol* class_name, TRAPS) { 119 ResourceMark rm(THREAD); 120 Handle pkgname_string; 121 TempNewSymbol pkg = ClassLoader::package_from_class_name(class_name); 122 if (pkg != nullptr) { // Package prefix found 123 const char* pkgname = pkg->as_klass_external_name(); 124 pkgname_string = java_lang_String::create_from_str(pkgname, 125 CHECK_(pkgname_string)); 126 } 127 return pkgname_string; 128 } 129 130 PackageEntry* CDSProtectionDomain::get_package_entry_from_class(InstanceKlass* ik, Handle class_loader) { 131 PackageEntry* pkg_entry = ik->package(); 132 if (CDSConfig::is_using_full_module_graph() && ik->is_shared() && pkg_entry != nullptr) { 133 assert(MetaspaceShared::is_in_shared_metaspace(pkg_entry), "must be"); 134 assert(!ik->is_shared_unregistered_class(), "unexpected archived package entry for an unregistered class"); 135 assert(ik->module()->is_named(), "unexpected archived package entry for a class in an unnamed module"); 136 return pkg_entry; 137 } 138 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(ik->name()); 139 if (pkg_name != nullptr) { 140 pkg_entry = ClassLoaderData::class_loader_data(class_loader())->packages()->lookup_only(pkg_name); 141 } else { 142 pkg_entry = nullptr; 143 } 144 return pkg_entry; 145 } 146 147 // Define Package for shared app classes from JAR file and also checks for 148 // package sealing (all done in Java code) 149 // See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html 150 void CDSProtectionDomain::define_shared_package(Symbol* class_name, 151 Handle class_loader, 152 Handle manifest, 153 Handle url, 154 TRAPS) { 155 assert(SystemDictionary::is_system_class_loader(class_loader()), "unexpected class loader"); 156 // get_package_name() returns a null handle if the class is in unnamed package 157 Handle pkgname_string = get_package_name(class_name, CHECK); 158 if (pkgname_string.not_null()) { 159 Klass* app_classLoader_klass = vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(); 160 JavaValue result(T_OBJECT); 161 JavaCallArguments args(3); 162 args.set_receiver(class_loader); 163 args.push_oop(pkgname_string); 164 args.push_oop(manifest); 165 args.push_oop(url); 166 JavaCalls::call_virtual(&result, app_classLoader_klass, 167 vmSymbols::defineOrCheckPackage_name(), 168 vmSymbols::defineOrCheckPackage_signature(), 169 &args, 170 CHECK); 171 } 172 } 173 174 Handle CDSProtectionDomain::create_jar_manifest(const char* manifest_chars, size_t size, TRAPS) { 175 typeArrayOop buf = oopFactory::new_byteArray((int)size, CHECK_NH); 176 typeArrayHandle bufhandle(THREAD, buf); 177 ArrayAccess<>::arraycopy_from_native(reinterpret_cast<const jbyte*>(manifest_chars), 178 buf, typeArrayOopDesc::element_offset<jbyte>(0), size); 179 Handle bais = JavaCalls::construct_new_instance(vmClasses::ByteArrayInputStream_klass(), 180 vmSymbols::byte_array_void_signature(), 181 bufhandle, CHECK_NH); 182 // manifest = new Manifest(ByteArrayInputStream) 183 Handle manifest = JavaCalls::construct_new_instance(vmClasses::Jar_Manifest_klass(), 184 vmSymbols::input_stream_void_signature(), 185 bais, CHECK_NH); 186 return manifest; 187 } 188 189 Handle CDSProtectionDomain::get_shared_jar_manifest(int shared_path_index, TRAPS) { 190 Handle manifest; 191 if (shared_jar_manifest(shared_path_index) == nullptr) { 192 SharedClassPathEntry* ent = FileMapInfo::shared_path(shared_path_index); 193 size_t size = (size_t)ent->manifest_size(); 194 if (size == 0) { 195 return Handle(); 196 } 197 198 // ByteArrayInputStream bais = new ByteArrayInputStream(buf); 199 const char* src = ent->manifest(); 200 assert(src != nullptr, "No Manifest data"); 201 manifest = create_jar_manifest(src, size, CHECK_NH); 202 atomic_set_shared_jar_manifest(shared_path_index, manifest()); 203 } 204 manifest = Handle(THREAD, shared_jar_manifest(shared_path_index)); 205 assert(manifest.not_null(), "sanity"); 206 return manifest; 207 } 208 209 Handle CDSProtectionDomain::get_shared_jar_url(int shared_path_index, TRAPS) { 210 Handle url_h; 211 if (shared_jar_url(shared_path_index) == nullptr) { 212 JavaValue result(T_OBJECT); 213 const char* path = FileMapInfo::shared_path_name(shared_path_index); 214 Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h)); 215 Klass* classLoaders_klass = 216 vmClasses::jdk_internal_loader_ClassLoaders_klass(); 217 JavaCalls::call_static(&result, classLoaders_klass, 218 vmSymbols::toFileURL_name(), 219 vmSymbols::toFileURL_signature(), 220 path_string, CHECK_(url_h)); 221 222 atomic_set_shared_jar_url(shared_path_index, result.get_oop()); 223 } 224 225 url_h = Handle(THREAD, shared_jar_url(shared_path_index)); 226 assert(url_h.not_null(), "sanity"); 227 return url_h; 228 } 229 230 Handle CDSProtectionDomain::get_archived_protection_domain(JavaThread* current, InstanceKlass* klass) { 231 oop pd = nullptr; 232 233 if (CDSConfig::is_loading_protection_domains()) { 234 oop mirror; 235 if (klass->has_archived_mirror_index()) { 236 mirror = klass->archived_java_mirror(); 237 } else { 238 mirror = klass->java_mirror(); 239 } 240 241 if (mirror != nullptr) { 242 pd = java_lang_Class::protection_domain(mirror); 243 } 244 } 245 246 if (log_is_enabled(Info, cds, protectiondomain)) { 247 ResourceMark rm; 248 log_info(cds, protectiondomain)("Archived protection domain for %s = %s", klass->external_name(), 249 (pd == nullptr) ? "none" : "found"); 250 } 251 252 if (pd == nullptr) { 253 return Handle(); 254 } else { 255 return Handle(current, pd); 256 } 257 } 258 259 // Get the ProtectionDomain associated with the CodeSource from the classloader. 260 Handle CDSProtectionDomain::get_protection_domain_from_classloader(Handle class_loader, 261 Handle url, TRAPS) { 262 // CodeSource cs = new CodeSource(url, null); 263 Handle cs = JavaCalls::construct_new_instance(vmClasses::CodeSource_klass(), 264 vmSymbols::url_code_signer_array_void_signature(), 265 url, Handle(), CHECK_NH); 266 267 // protection_domain = SecureClassLoader.getProtectionDomain(cs); 268 Klass* secureClassLoader_klass = vmClasses::SecureClassLoader_klass(); 269 JavaValue obj_result(T_OBJECT); 270 JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass, 271 vmSymbols::getProtectionDomain_name(), 272 vmSymbols::getProtectionDomain_signature(), 273 cs, CHECK_NH); 274 return Handle(THREAD, obj_result.get_oop()); 275 } 276 277 // Returns the ProtectionDomain associated with the JAR file identified by the url. 278 Handle CDSProtectionDomain::get_shared_protection_domain(Handle class_loader, 279 int shared_path_index, 280 Handle url, 281 TRAPS) { 282 Handle protection_domain; 283 if (shared_protection_domain(shared_path_index) == nullptr) { 284 Handle pd = get_protection_domain_from_classloader(class_loader, url, CHECK_NH); 285 atomic_set_shared_protection_domain(shared_path_index, pd()); 286 } 287 288 // Acquire from the cache because if another thread beats the current one to 289 // set the shared protection_domain and the atomic_set fails, the current thread 290 // needs to get the updated protection_domain from the cache. 291 protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index)); 292 assert(protection_domain.not_null(), "sanity"); 293 return protection_domain; 294 } 295 296 // Returns the ProtectionDomain associated with the moduleEntry. 297 Handle CDSProtectionDomain::get_shared_protection_domain(Handle class_loader, 298 ModuleEntry* mod, TRAPS) { 299 ClassLoaderData *loader_data = mod->loader_data(); 300 if (mod->shared_protection_domain() == nullptr) { 301 Symbol* location = mod->location(); 302 if (location != nullptr) { 303 Handle location_string = java_lang_String::create_from_symbol( 304 location, CHECK_NH); 305 Handle url; 306 JavaValue result(T_OBJECT); 307 if (location->starts_with("jrt:/")) { 308 url = JavaCalls::construct_new_instance(vmClasses::URL_klass(), 309 vmSymbols::string_void_signature(), 310 location_string, CHECK_NH); 311 } else { 312 Klass* classLoaders_klass = 313 vmClasses::jdk_internal_loader_ClassLoaders_klass(); 314 JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(), 315 vmSymbols::toFileURL_signature(), 316 location_string, CHECK_NH); 317 url = Handle(THREAD, result.get_oop()); 318 } 319 320 Handle pd = get_protection_domain_from_classloader(class_loader, url, 321 CHECK_NH); 322 mod->set_shared_protection_domain(loader_data, pd); 323 } 324 } 325 326 Handle protection_domain(THREAD, mod->shared_protection_domain()); 327 assert(protection_domain.not_null(), "sanity"); 328 return protection_domain; 329 } 330 331 void CDSProtectionDomain::atomic_set_array_index(OopHandle array, int index, oop o) { 332 // Benign race condition: array.obj_at(index) may already be filled in. 333 // The important thing here is that all threads pick up the same result. 334 // It doesn't matter which racing thread wins, as long as only one 335 // result is used by all threads, and all future queries. 336 ((objArrayOop)array.resolve())->replace_if_null(index, o); 337 } 338 339 oop CDSProtectionDomain::shared_protection_domain(int index) { 340 return ((objArrayOop)_shared_protection_domains.resolve())->obj_at(index); 341 } 342 343 void CDSProtectionDomain::allocate_shared_protection_domain_array(int size, TRAPS) { 344 if (_shared_protection_domains.resolve() == nullptr) { 345 oop spd = oopFactory::new_objArray( 346 vmClasses::ProtectionDomain_klass(), size, CHECK); 347 _shared_protection_domains = OopHandle(Universe::vm_global(), spd); 348 } 349 } 350 351 oop CDSProtectionDomain::shared_jar_url(int index) { 352 return ((objArrayOop)_shared_jar_urls.resolve())->obj_at(index); 353 } 354 355 void CDSProtectionDomain::allocate_shared_jar_url_array(int size, TRAPS) { 356 if (_shared_jar_urls.resolve() == nullptr) { 357 oop sju = oopFactory::new_objArray( 358 vmClasses::URL_klass(), size, CHECK); 359 _shared_jar_urls = OopHandle(Universe::vm_global(), sju); 360 } 361 } 362 363 oop CDSProtectionDomain::shared_jar_manifest(int index) { 364 return ((objArrayOop)_shared_jar_manifests.resolve())->obj_at(index); 365 } 366 367 void CDSProtectionDomain::allocate_shared_jar_manifest_array(int size, TRAPS) { 368 if (_shared_jar_manifests.resolve() == nullptr) { 369 oop sjm = oopFactory::new_objArray( 370 vmClasses::Jar_Manifest_klass(), size, CHECK); 371 _shared_jar_manifests = OopHandle(Universe::vm_global(), sjm); 372 } 373 }