1 /* 2 * Copyright (c) Christos Zoulas 2021. 3 * All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <sys/mman.h> 31 #include <stdio.h> 32 #include <stdbool.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <unistd.h> 38 #include <dlfcn.h> 39 #include <magic.h> 40 41 void * 42 malloc(size_t len) 43 { 44 char buf[512]; 45 void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc"); 46 void *p = (*orig)(len); 47 int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p); 48 write(2, buf, l); 49 return p; 50 } 51 52 void 53 free(void *p) 54 { 55 char buf[512]; 56 void (*orig)(void *) = dlsym(RTLD_NEXT, "free"); 57 (*orig)(p); 58 int l = snprintf(buf, sizeof(buf), "free %p\n", p); 59 write(2, buf, l); 60 } 61 62 void * 63 calloc(size_t len, size_t nitems) 64 { 65 char buf[512]; 66 void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc"); 67 void *p = (*orig)(len, nitems); 68 size_t tot = len * nitems; 69 int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p); 70 write(2, buf, l); 71 return p; 72 } 73 void * 74 realloc(void *q, size_t len) 75 { 76 char buf[512]; 77 void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc"); 78 void *p = (*orig)(q, len); 79 int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p); 80 write(2, buf, l); 81 return p; 82 } 83 84 static void 85 usage(void) 86 { 87 fprintf(stderr, "Usage: test [-b] <filename>\n"); 88 exit(EXIT_FAILURE); 89 } 90 91 int 92 main(int argc, char *argv[]) 93 { 94 bool buf = false; 95 int c; 96 97 while ((c = getopt(argc, argv, "b")) != -1) 98 switch (c) { 99 case 'b': 100 buf = true; 101 break; 102 default: 103 usage(); 104 } 105 106 argc -= optind; 107 argv += optind; 108 109 if (argc == 0) 110 usage(); 111 112 magic_t m = magic_open(0); 113 if (m == NULL) 114 err(EXIT_FAILURE, "magic_open"); 115 116 magic_load(m, NULL); 117 118 const char *r; 119 if (buf) { 120 int fd = open(argv[0], O_RDONLY); 121 if (fd == -1) 122 err(EXIT_FAILURE, "Cannot open `%s'", argv[0]); 123 124 struct stat st; 125 if (fstat(fd, &st) == -1) 126 err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]); 127 size_t l = (size_t)st.st_size; 128 void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 129 (off_t)0); 130 if (p == MAP_FAILED) 131 err(EXIT_FAILURE, "Cannot map `%s'", argv[0]); 132 close(fd); 133 r = magic_buffer(m, p, l); 134 munmap(p, l); 135 } else { 136 r = magic_file(m, argv[0]); 137 } 138 magic_close(m); 139 140 printf("%s\n", r ? r : "(null)"); 141 142 return 0; 143 } 144