@@ -249,6 +249,25 @@ static std::vector<std::string> getVintfUpdatableNames(const std::string& apexNa
249249 return names;
250250}
251251
252+ static std::optional<std::string> getVintfAccessorName (const std::string& name) {
253+ AidlName aname;
254+ if (!AidlName::fill (name, &aname)) return std::nullopt ;
255+
256+ std::optional<std::string> accessor;
257+ forEachManifest ([&](const ManifestWithDescription& mwd) {
258+ mwd.manifest ->forEachInstance ([&](const auto & manifestInstance) {
259+ if (manifestInstance.format () != vintf::HalFormat::AIDL) return true ;
260+ if (manifestInstance.package () != aname.package ) return true ;
261+ if (manifestInstance.interface () != aname.iface ) return true ;
262+ if (manifestInstance.instance () != aname.instance ) return true ;
263+ accessor = manifestInstance.accessor ();
264+ return false ; // break (libvintf uses opposite convention)
265+ });
266+ return false ; // continue
267+ });
268+ return accessor;
269+ }
270+
252271static std::optional<ConnectionInfo> getVintfConnectionInfo (const std::string& name) {
253272 AidlName aname;
254273 if (!AidlName::fill (name, &aname)) return std::nullopt ;
@@ -364,23 +383,40 @@ ServiceManager::~ServiceManager() {
364383 }
365384}
366385
367- Status ServiceManager::getService (const std::string& name, sp<IBinder>* outBinder ) {
386+ Status ServiceManager::getService (const std::string& name, os::Service* outService ) {
368387 SM_PERFETTO_TRACE_FUNC (PERFETTO_TE_ARG_STRING (" name" , name.c_str ()));
369388
370- *outBinder = tryGetService (name, true );
389+ *outService = tryGetService (name, true );
371390 // returns ok regardless of result for legacy reasons
372391 return Status::ok ();
373392}
374393
375- Status ServiceManager::checkService (const std::string& name, sp<IBinder>* outBinder ) {
394+ Status ServiceManager::checkService (const std::string& name, os::Service* outService ) {
376395 SM_PERFETTO_TRACE_FUNC (PERFETTO_TE_ARG_STRING (" name" , name.c_str ()));
377396
378- *outBinder = tryGetService (name, false );
397+ *outService = tryGetService (name, false );
379398 // returns ok regardless of result for legacy reasons
380399 return Status::ok ();
381400}
382401
383- sp<IBinder> ServiceManager::tryGetService (const std::string& name, bool startIfNotFound) {
402+ os::Service ServiceManager::tryGetService (const std::string& name, bool startIfNotFound) {
403+ std::optional<std::string> accessorName;
404+ #ifndef VENDORSERVICEMANAGER
405+ accessorName = getVintfAccessorName (name);
406+ #endif
407+ if (accessorName.has_value ()) {
408+ auto ctx = mAccess ->getCallingContext ();
409+ if (!mAccess ->canFind (ctx, name)) {
410+ return os::Service::make<os::Service::Tag::accessor>(nullptr );
411+ }
412+ return os::Service::make<os::Service::Tag::accessor>(
413+ tryGetBinder (*accessorName, startIfNotFound));
414+ } else {
415+ return os::Service::make<os::Service::Tag::binder>(tryGetBinder (name, startIfNotFound));
416+ }
417+ }
418+
419+ sp<IBinder> ServiceManager::tryGetBinder (const std::string& name, bool startIfNotFound) {
384420 SM_PERFETTO_TRACE_FUNC (PERFETTO_TE_ARG_STRING (" name" , name.c_str ()));
385421
386422 auto ctx = mAccess ->getCallingContext ();
@@ -565,8 +601,11 @@ Status ServiceManager::registerForNotifications(
565601
566602 auto ctx = mAccess ->getCallingContext ();
567603
568- if (!mAccess ->canFind (ctx, name)) {
569- return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux" );
604+ // TODO(b/338541373): Implement the notification mechanism for services accessed via
605+ // IAccessor.
606+ std::optional<std::string> accessorName;
607+ if (auto status = canFindService (ctx, name, &accessorName); !status.isOk ()) {
608+ return status;
570609 }
571610
572611 // note - we could allow isolated apps to get notifications if we
@@ -613,8 +652,9 @@ Status ServiceManager::unregisterForNotifications(
613652
614653 auto ctx = mAccess ->getCallingContext ();
615654
616- if (!mAccess ->canFind (ctx, name)) {
617- return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux denied." );
655+ std::optional<std::string> accessorName;
656+ if (auto status = canFindService (ctx, name, &accessorName); !status.isOk ()) {
657+ return status;
618658 }
619659
620660 bool found = false ;
@@ -638,8 +678,9 @@ Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
638678
639679 auto ctx = mAccess ->getCallingContext ();
640680
641- if (!mAccess ->canFind (ctx, name)) {
642- return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux denied." );
681+ std::optional<std::string> accessorName;
682+ if (auto status = canFindService (ctx, name, &accessorName); !status.isOk ()) {
683+ return status;
643684 }
644685
645686 *outReturn = false ;
@@ -662,8 +703,10 @@ binder::Status ServiceManager::getDeclaredInstances(const std::string& interface
662703
663704 outReturn->clear ();
664705
706+ std::optional<std::string> _accessorName;
665707 for (const std::string& instance : allInstances) {
666- if (mAccess ->canFind (ctx, interface + " /" + instance)) {
708+ if (auto status = canFindService (ctx, interface + " /" + instance, &_accessorName);
709+ status.isOk ()) {
667710 outReturn->push_back (instance);
668711 }
669712 }
@@ -681,8 +724,9 @@ Status ServiceManager::updatableViaApex(const std::string& name,
681724
682725 auto ctx = mAccess ->getCallingContext ();
683726
684- if (!mAccess ->canFind (ctx, name)) {
685- return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux denied." );
727+ std::optional<std::string> _accessorName;
728+ if (auto status = canFindService (ctx, name, &_accessorName); !status.isOk ()) {
729+ return status;
686730 }
687731
688732 *outReturn = std::nullopt ;
@@ -706,8 +750,9 @@ Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& ape
706750
707751 outReturn->clear ();
708752
753+ std::optional<std::string> _accessorName;
709754 for (const std::string& name : apexUpdatableNames) {
710- if (mAccess -> canFind (ctx, name)) {
755+ if (auto status = canFindService (ctx, name, &_accessorName); status. isOk ( )) {
711756 outReturn->push_back (name);
712757 }
713758 }
@@ -724,8 +769,9 @@ Status ServiceManager::getConnectionInfo(const std::string& name,
724769
725770 auto ctx = mAccess ->getCallingContext ();
726771
727- if (!mAccess ->canFind (ctx, name)) {
728- return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux denied." );
772+ std::optional<std::string> _accessorName;
773+ if (auto status = canFindService (ctx, name, &_accessorName); !status.isOk ()) {
774+ return status;
729775 }
730776
731777 *outReturn = std::nullopt ;
@@ -1032,6 +1078,23 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
10321078 return Status::ok ();
10331079}
10341080
1081+ Status ServiceManager::canFindService (const Access::CallingContext& ctx, const std::string& name,
1082+ std::optional<std::string>* accessor) {
1083+ if (!mAccess ->canFind (ctx, name)) {
1084+ return Status::fromExceptionCode (Status::EX_SECURITY, " SELinux denied for service." );
1085+ }
1086+ #ifndef VENDORSERVICEMANAGER
1087+ *accessor = getVintfAccessorName (name);
1088+ #endif
1089+ if (accessor->has_value ()) {
1090+ if (!mAccess ->canFind (ctx, accessor->value ())) {
1091+ return Status::fromExceptionCode (Status::EX_SECURITY,
1092+ " SELinux denied for the accessor of the service." );
1093+ }
1094+ }
1095+ return Status::ok ();
1096+ }
1097+
10351098Status ServiceManager::getServiceDebugInfo (std::vector<ServiceDebugInfo>* outReturn) {
10361099 SM_PERFETTO_TRACE_FUNC ();
10371100 if (!mAccess ->canList (mAccess ->getCallingContext ())) {
0 commit comments