• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dlfcn.h>
19 #include <fcntl.h>
20 #include <ipp.h>
21 
22 int isInitialized = 0;
23 void *check_ptr = NULL;
24 size_t text_len = sizeof("text/plain") - 1;
25 
26 static void *(*real_malloc)(size_t) = NULL;
27 static int (*real_strcmp)(const char *str1, const char *str2) = NULL;
28 
init(void)29 void init(void) {
30   real_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
31   if (real_malloc == NULL) {
32     return;
33   }
34   real_strcmp = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "strcmp");
35   if (real_strcmp == NULL) {
36     return;
37   }
38   isInitialized = 1;
39 }
40 
malloc(size_t size)41 void *malloc(size_t size) {
42   if (!isInitialized) {
43     init();
44   }
45   void *tmp = real_malloc(size);
46   if (size == text_len) {
47     check_ptr = tmp;
48   }
49   return tmp;
50 }
51 
strcmp(const char * str1,const char * str2)52 int strcmp(const char *str1, const char *str2) {
53   if (!isInitialized) {
54     init();
55   }
56   if ((str1 == check_ptr) && (str1[text_len - 1] != '\0')) {
57     exit(EXIT_VULNERABLE);
58   }
59   return real_strcmp(str1, str2);
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char **argv) {
63   if (argc < 2) {
64     return EXIT_FAILURE;
65   }
66 
67   int file_desc = open((const char *)argv[1], O_RDONLY);
68   if (file_desc < 0) {
69     return EXIT_FAILURE;
70   }
71 
72   ipp_t *job = ippNew();
73   if (!job) {
74     return EXIT_FAILURE;
75   }
76   ippReadFile(file_desc, job);
77 
78   if (job) {
79     free(job);
80   }
81   close(file_desc);
82   return EXIT_SUCCESS;
83 }
84