• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "binary_loader.h"
19 
20 size_t kItems = 2;
21 size_t kSize = SIZE_MAX;
22 void* kAddress = &kItems;
23 
24 typedef void* (*skia_alloc_func)(void*, unsigned long, unsigned long);
25 
calloc(size_t item,size_t size)26 void* calloc(size_t item, size_t size) {
27     if (item * size == kItems * kSize) {
28         return kAddress;
29     }
30 
31     // memset to imitate actual calloc() call
32     void* ptr = malloc(item * size);
33     memset(ptr, 0, item * size);
34     return ptr;
35 }
36 
main(int,char * argv[])37 int main(int /* argc */, char* argv[]) {
38     // Get the path to the shared library and offset from command-line arguments
39     const char* libPath = argv[1];
40     const uintptr_t functionOffset = strtoul(argv[2], nullptr, 0) + 1;
41 
42     // Get function address of 'skia_alloc_func()' from loaded library
43     BinaryLoader binaryLoader(libPath);
44     const uintptr_t functionAddress = binaryLoader.getFunctionAddress(functionOffset);
45     FAIL_CHECK(functionAddress);
46 
47     // Create function pointer to 'skia_alloc_func()'
48     skia_alloc_func skia_alloc_ptr = (skia_alloc_func)functionAddress;
49 
50     // Call vulnerable function. Without fix, function returns pointer from the overloaded calloc().
51     // With Fix, null pointer is returned.
52     void* result = skia_alloc_ptr(nullptr, kItems, kSize);
53     if (result == nullptr) {
54         return EXIT_SUCCESS;
55     }
56     if (result == kAddress) {
57         return EXIT_VULNERABLE;
58     }
59     return EXIT_FAILURE;
60 }
61