1 /* 2 * Copyright (C) 2007 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 #pragma once 18 19 #include <stdint.h> 20 #include <sys/types.h> 21 #include <sys/mman.h> 22 23 #include <utils/RefBase.h> 24 #include <utils/Errors.h> 25 #include <binder/Common.h> 26 #include <binder/IInterface.h> 27 28 namespace android { 29 30 // ---------------------------------------------------------------------------- 31 32 class LIBBINDER_EXPORTED IMemoryHeap : public IInterface { 33 public: 34 DECLARE_META_INTERFACE(MemoryHeap) 35 36 // flags returned by getFlags() 37 enum { 38 READ_ONLY = 0x00000001 39 }; 40 41 virtual int getHeapID() const = 0; 42 virtual void* getBase() const = 0; 43 virtual size_t getSize() const = 0; 44 virtual uint32_t getFlags() const = 0; 45 virtual off_t getOffset() const = 0; 46 47 // these are there just for backward source compatibility heapID()48 int32_t heapID() const { return getHeapID(); } base()49 void* base() const { return getBase(); } virtualSize()50 size_t virtualSize() const { return getSize(); } 51 }; 52 53 class LIBBINDER_EXPORTED BnMemoryHeap : public BnInterface<IMemoryHeap> { 54 public: 55 // NOLINTNEXTLINE(google-default-arguments) 56 virtual status_t onTransact( 57 uint32_t code, 58 const Parcel& data, 59 Parcel* reply, 60 uint32_t flags = 0); 61 62 BnMemoryHeap(); 63 protected: 64 virtual ~BnMemoryHeap(); 65 }; 66 67 // ---------------------------------------------------------------------------- 68 69 class LIBBINDER_EXPORTED IMemory : public IInterface { 70 public: 71 DECLARE_META_INTERFACE(Memory) 72 73 // NOLINTNEXTLINE(google-default-arguments) 74 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=nullptr, size_t* size=nullptr) const = 0; 75 76 // helpers 77 78 // Accessing the underlying pointer must be done with caution, as there are 79 // some inherent security risks associated with it. When receiving an 80 // IMemory from an untrusted process, there is currently no way to guarantee 81 // that this process would't change the content after the fact. This may 82 // lead to TOC/TOU class of security bugs. In most cases, when performance 83 // is not an issue, the recommended practice is to immediately copy the 84 // buffer upon reception, then work with the copy, e.g.: 85 // 86 // std::string private_copy(mem.size(), '\0'); 87 // memcpy(private_copy.data(), mem.unsecurePointer(), mem.size()); 88 // 89 // In cases where performance is an issue, this matter must be addressed on 90 // an ad-hoc basis. 91 void* unsecurePointer() const; 92 93 size_t size() const; 94 ssize_t offset() const; 95 96 private: 97 // These are now deprecated and are left here for backward-compatibility 98 // with prebuilts that may reference these symbol at runtime. 99 // Instead, new code should use unsecurePointer()/unsecureFastPointer(), 100 // which do the same thing, but make it more obvious that there are some 101 // security-related pitfalls associated with them. 102 void* pointer() const; 103 void* fastPointer(const sp<IBinder>& heap, ssize_t offset) const; 104 }; 105 106 class LIBBINDER_EXPORTED BnMemory : public BnInterface<IMemory> { 107 public: 108 // NOLINTNEXTLINE(google-default-arguments) 109 virtual status_t onTransact( 110 uint32_t code, 111 const Parcel& data, 112 Parcel* reply, 113 uint32_t flags = 0); 114 115 BnMemory(); 116 protected: 117 virtual ~BnMemory(); 118 }; 119 120 // ---------------------------------------------------------------------------- 121 122 } // namespace android 123