1 /**
2  * Copyright (C) 2022 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 // This PoC is written taking reference from
18 // frameworks/base/native/graphics/jni/imagedecoder.cpp
19 
20 #include "../includes/common.h"
21 #include <android/imagedecoder.h>
22 #include <binder/IPCThreadState.h>
23 #include <vector>
24 
25 bool testInProgress = false;
26 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)27 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
28   if (testInProgress && info->si_signo == SIGSEGV) {
29     (*old_action.sa_sigaction)(signum, info, context);
30     return;
31   }
32   exit(EXIT_FAILURE);
33 }
34 
main(int argc,char ** argv)35 int main(int argc, char **argv) {
36   FAIL_CHECK(argc >= 2);
37   sigemptyset(&new_action.sa_mask);
38   new_action.sa_flags = SA_SIGINFO;
39   new_action.sa_sigaction = sigsegv_handler;
40   sigaction(SIGSEGV, &new_action, &old_action);
41   android::ProcessState::self()->startThreadPool();
42   FILE *file = fopen(argv[1], "r");
43   FAIL_CHECK(file);
44   fseek(file, 0, SEEK_END);
45   size_t size = ftell(file);
46   fseek(file, 0, SEEK_SET);
47   std::vector<uint8_t> buffer(size);
48   fread((void *)buffer.data(), 1, size, file);
49   fclose(file);
50   testInProgress = true;
51   AImageDecoder *decoder;
52   if (AImageDecoder_createFromBuffer(buffer.data(), size, &decoder) ==
53       ANDROID_IMAGE_DECODER_SUCCESS) {
54     AImageDecoder_delete(decoder);
55   }
56   testInProgress = false;
57   FAIL_CHECK(decoder);
58   return EXIT_SUCCESS;
59 }
60