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