1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <media/ValidatedAttributionSourceState.h>
18 
19 #include <binder/IPCThreadState.h>
20 #include <error/expected_utils.h>
21 #include <utils/Log.h>
22 
23 using ::android::binder::Status;
24 using ::android::error::BinderResult;
25 using ::android::error::unexpectedExceptionCode;
26 
27 namespace com::android::media::permission {
28 
29 BinderResult<ValidatedAttributionSourceState>
createFromBinderContext(AttributionSourceState attr,const IPermissionProvider & provider)30 ValidatedAttributionSourceState::createFromBinderContext(AttributionSourceState attr,
31                                                          const IPermissionProvider& provider) {
32     attr.pid = ::android::IPCThreadState::self()->getCallingPid();
33     attr.uid = ::android::IPCThreadState::self()->getCallingUid();
34     return createFromTrustedUidNoPackage(std::move(attr), provider);
35 }
36 
37 BinderResult<ValidatedAttributionSourceState>
createFromTrustedUidNoPackage(AttributionSourceState attr,const IPermissionProvider & provider)38 ValidatedAttributionSourceState::createFromTrustedUidNoPackage(
39         AttributionSourceState attr, const IPermissionProvider& provider) {
40     if (attr.packageName.has_value() && attr.packageName->size() != 0) {
41         if (VALUE_OR_RETURN(provider.validateUidPackagePair(attr.uid, attr.packageName.value()))) {
42             return ValidatedAttributionSourceState{std::move(attr)};
43         } else {
44             return unexpectedExceptionCode(Status::EX_SECURITY,
45                                            attr.toString()
46                                                    .insert(0, ": invalid attr ")
47                                                    .insert(0, __PRETTY_FUNCTION__)
48                                                    .c_str());
49         }
50     } else {
51         // For APIs which don't appropriately pass attribution sources or packages, we need
52         // to populate the package name with our best guess.
53         const auto packageNames = VALUE_OR_RETURN(provider.getPackagesForUid(attr.uid));
54         LOG_ALWAYS_FATAL_IF(packageNames.empty(), "%s BUG: empty package list from controller",
55                             __PRETTY_FUNCTION__);
56         attr.packageName = std::move(packageNames[0]);
57         return ValidatedAttributionSourceState{std::move(attr)};
58     }
59 }
60 
61 }  // namespace com::android::media::permission
62