Enrico Granata | e23f034 | 2017-10-23 17:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "server.h" |
| 18 | |
| 19 | #include <binder/IPCThreadState.h> |
| 20 | |
| 21 | #include <private/android_filesystem_config.h> |
| 22 | |
| 23 | static bool isSystemUser() { |
Nicholas Sauer | 771aade | 2018-10-22 15:42:01 -0700 | [diff] [blame] | 24 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 25 | uid_t aid = uid % AID_USER_OFFSET; |
| 26 | switch (aid) { |
Enrico Granata | e23f034 | 2017-10-23 17:17:52 -0700 | [diff] [blame] | 27 | case AID_ROOT: |
| 28 | case AID_SYSTEM: { |
| 29 | return true; |
| 30 | } break; |
| 31 | default: { |
Nicholas Sauer | 771aade | 2018-10-22 15:42:01 -0700 | [diff] [blame] | 32 | ALOGE("aid %u (uid %u) is not root nor system - access denied", aid, uid); |
Enrico Granata | e23f034 | 2017-10-23 17:17:52 -0700 | [diff] [blame] | 33 | } break; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | namespace procfsinspector { |
| 39 | class BpProcfsInspector: public BpInterface<IProcfsInspector> { |
| 40 | public: |
| 41 | BpProcfsInspector(sp<IBinder> binder) : BpInterface<IProcfsInspector>(binder) {} |
| 42 | |
| 43 | virtual std::vector<ProcessInfo> readProcessTable() override { |
| 44 | Parcel data, reply; |
| 45 | remote()->transact((uint32_t)IProcfsInspector::Call::READ_PROCESS_TABLE, data, &reply); |
| 46 | |
| 47 | std::vector<procfsinspector::ProcessInfo> result; |
| 48 | reply.readParcelableVector(&result); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | }; |
| 53 | |
| 54 | IMPLEMENT_META_INTERFACE(ProcfsInspector, "com.android.car.procfsinspector.IProcfsInspector"); |
| 55 | |
| 56 | status_t Impl::onTransact(uint32_t code, |
| 57 | const Parcel& data, Parcel* reply, uint32_t flags) { |
| 58 | |
| 59 | if (code == (uint32_t)IProcfsInspector::Call::READ_PROCESS_TABLE) { |
| 60 | CHECK_INTERFACE(IProcfsInspector, data, reply); |
| 61 | if (isSystemUser()) { |
| 62 | reply->writeNoException(); |
| 63 | reply->writeParcelableVector(readProcessTable()); |
| 64 | return NO_ERROR; |
| 65 | } else { |
| 66 | return PERMISSION_DENIED; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return BBinder::onTransact(code, data, reply, flags); |
| 71 | } |
| 72 | |
| 73 | } |