Skip to content

Instantly share code, notes, and snippets.

@darealshinji
Last active October 27, 2023 22:54
Show Gist options
  • Save darealshinji/c3ece5ec7d3ac580793c578928bee0a6 to your computer and use it in GitHub Desktop.
Save darealshinji/c3ece5ec7d3ac580793c578928bee0a6 to your computer and use it in GitHub Desktop.
find the highest version GLIBC_* symbol in a file or directory
/* Public Domain code */
/* find the highest version GLIBC_* symbol in a file or directory */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <ctype.h>
#include <elf.h>
#include <fcntl.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define _ELFTYPE(x,n) typedef Elf ## n ## _ ## x Elf_ ## x
#ifdef __LP64__
#define ELFCLASS_CURRENT ELFCLASS64
#define ELFTYPE(x) _ELFTYPE(x, 64)
#else
#define ELFCLASS_CURRENT ELFCLASS32
#define ELFTYPE(x) _ELFTYPE(x, 32)
#endif
ELFTYPE(Ehdr);
ELFTYPE(Shdr);
ELFTYPE(Sym);
ELFTYPE(Off);
#define PRNT(...) if (verbose) printf(__VA_ARGS__)
char sym_found[32] = {0};
char path_found[4096] = {0};
int verbose = 0;
const char *lookup_symbol(const char *addr, const size_t fsize)
{
static char buf[32] = {0};
char *sym = NULL;
#define CHECK_SIZE(x) if ((x) >= fsize) return NULL
/* check magic bytes */
if (memcmp(addr, ELFMAG, SELFMAG) != 0) {
return NULL;
}
Elf_Ehdr *ehdr = (Elf_Ehdr *)addr;
/* check size, class and version */
CHECK_SIZE(ehdr->e_shoff);
if (ehdr->e_ident[EI_CLASS] != ELFCLASS_CURRENT ||
ehdr->e_ident[EI_VERSION] != EV_CURRENT)
{
return NULL;
}
/* section header string table */
Elf_Shdr *shdr = (Elf_Shdr *)(addr + ehdr->e_shoff);
Elf_Shdr *sh_strtab = &shdr[ehdr->e_shstrndx];
CHECK_SIZE(sh_strtab->sh_offset);
const char *strtab = addr + sh_strtab->sh_offset;
Elf_Shdr *sh_dynstr = NULL;
/* iterate through sections to find .dynstr */
for (size_t i = 0; i < ehdr->e_shnum; i++) {
CHECK_SIZE(sh_strtab->sh_offset + shdr[i].sh_name);
if (memcmp(strtab + shdr[i].sh_name, ".dynstr\0", 8) == 0) {
sh_dynstr = &shdr[i];
break;
}
}
if (!sh_dynstr) return NULL;
CHECK_SIZE(sh_dynstr->sh_offset + sh_dynstr->sh_size);
const char *ptr = addr + sh_dynstr->sh_offset;
const char *start = ptr;
const char * const end = ptr + sh_dynstr->sh_size;
if (*end) return NULL;
/* parse .dynstr section data for GLIBC_* symbols */
for ( ; ptr < end; ptr++) {
if (*ptr) continue;
if (strncmp(start, "GLIBC_", 6) == 0 &&
isdigit(start[6]) &&
(!sym || strverscmp(sym, start) < 0))
{
strncpy(buf, start, sizeof(buf)-1);
sym = buf;
PRNT("%s ", sym+6);
}
start = ptr + 1;
}
return sym;
}
int ftw_callback(const char *fpath, const struct stat *sb, int typeflag, struct FTW *unused)
{
(void)unused;
/* check only regular files */
if (typeflag != FTW_F) return 0;
if (sb->st_size < (off_t)(sizeof(Elf_Ehdr))) {
return 0;
}
/* open() file */
int fd = open(fpath, O_RDONLY);
if (fd < 0) return 0;
/* mmap() library */
void *addr = mmap(NULL, sb->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
perror("mmap()");
close(fd);
return 1;
}
/* find symbol */
const char *sym = lookup_symbol((const char *)addr, sb->st_size);
/* free resources */
munmap(addr, sb->st_size);
close(fd);
if (sym) {
PRNT("-> %s\n%s\n\n", sym, fpath);
} else {
return 0;
}
/* compare version strings */
if (*sym_found == 0 || strverscmp(sym_found, sym) < 0) {
strncpy(sym_found, sym, sizeof(sym_found)-1);
strncpy(path_found, fpath, sizeof(path_found)-1);
}
return 0;
}
int main(int argc, char **argv)
{
const char *path = NULL;
if (argc == 3 && strcmp(argv[1], "-v") == 0) {
path = argv[2];
verbose = 1;
} else if (argc == 2) {
path = argv[1];
} else {
printf("usage: %s [-v] <FILE|DIRECTORY>\n", argv[0]);
return 1;
}
if (nftw(path, ftw_callback, 20, FTW_MOUNT | FTW_PHYS) != 0) {
perror("nftw()");
exit(1);
}
PRNT("-----------------------------------------\n\n");
printf("%s\n%s\n", sym_found, path_found);
return 0;
}
/* $NetBSD: popen.c,v 1.38 2022/04/19 20:32:15 rillig Exp $ */
/* provide popenvp(), a modified version of popenve(3) that uses execvp(3) */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software written by Ken Arnold and
* published in UNIX Review, Vol. 6, No. 8.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define USAGE_TEXT \
"Use objdump to find the highest GLIBC symbol version in an ELF file.\n\n" \
"Usage: %s [--use-readelf] <FILE|DIRECTORY>\n"
#ifdef _GNU_SOURCE
#undef _GNU_SOURCE
#endif
#define _GNU_SOURCE
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <ftw.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define FORK_ERROR -1
#define FORK_CHILD 0
static struct pid {
struct pid *next;
FILE *fp;
pid_t pid;
} *pidlist;
static struct pid *
pdes_get(int *pdes, const char **type)
{
struct pid *cur;
int flags = strchr(*type, 'e') ? O_CLOEXEC : 0;
int serrno;
if (strchr(*type, '+')) {
int stype = flags ? (SOCK_STREAM | SOCK_CLOEXEC) : SOCK_STREAM;
*type = "r+";
if (socketpair(AF_LOCAL, stype, 0, pdes) < 0)
return NULL;
} else {
*type = strrchr(*type, 'r') ? "r" : "w";
if (pipe2(pdes, flags) == -1)
return NULL;
}
if ((cur = malloc(sizeof(*cur))) != NULL)
return cur;
serrno = errno;
(void)close(pdes[0]);
(void)close(pdes[1]);
errno = serrno;
return NULL;
}
static void
pdes_child(int *pdes, const char *type)
{
struct pid *old;
/* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
from previous popen() calls that remain open in the
parent process are closed in the new child process. */
for (old = pidlist; old; old = old->next)
(void)close(fileno(old->fp)); /* don't allow a flush */
if (type[0] == 'r') {
(void)close(pdes[0]);
if (pdes[1] != STDOUT_FILENO) {
(void)dup2(pdes[1], STDOUT_FILENO);
(void)close(pdes[1]);
}
if (type[1] == '+')
(void)dup2(STDOUT_FILENO, STDIN_FILENO);
} else {
(void)close(pdes[1]);
if (pdes[0] != STDIN_FILENO) {
(void)dup2(pdes[0], STDIN_FILENO);
(void)close(pdes[0]);
}
}
}
static void
pdes_parent(int *pdes, struct pid *cur, pid_t pid, const char *type)
{
FILE *iop;
/* Parent; assume fdopen can't fail. */
if (*type == 'r') {
iop = fdopen(pdes[0], type);
(void)close(pdes[1]);
} else {
iop = fdopen(pdes[1], type);
(void)close(pdes[0]);
}
/* Link into list of file descriptors. */
cur->fp = iop;
cur->pid = pid;
cur->next = pidlist;
pidlist = cur;
}
static void
pdes_error(int *pdes, struct pid *cur)
{
free(cur);
(void)close(pdes[0]);
(void)close(pdes[1]);
}
/* modified version of popenve(3) that uses execvp(3) */
FILE *
popenvp(const char *cmd, char *const *argv, const char *type, int disable_child_stderr)
{
struct pid *cur;
int pdes[2], serrno;
pid_t pid;
if ((cur = pdes_get(pdes, &type)) == NULL)
return NULL;
switch (pid = vfork()) {
case FORK_ERROR:
serrno = errno;
pdes_error(pdes, cur);
errno = serrno;
return NULL;
/* NOTREACHED */
case FORK_CHILD:
if (disable_child_stderr) {
close(STDERR_FILENO);
}
pdes_child(pdes, type);
execvp(cmd, argv);
_exit(127);
/* NOTREACHED */
}
pdes_parent(pdes, cur, pid, type);
return cur->fp;
}
/******************** end of popen.c ********************/
static int use_readelf = 0;
static char glibc[16] = {0};
static char glibc_file[4096] = {0};
static void strip_glibc_str(char *p)
{
for ( ; *p != 0; p++) {
if (isdigit(*p) || *p == '.') {
continue;
}
*p = 0;
return;
}
}
static int is_objdump_delim(int c) {
return isspace(c);
}
static int is_readelf_delim(int c) {
return (c == '@');
}
const char *glibc_version(const char *file)
{
int c;
char buf[24] = {0};
static char out[16] = {0};
typedef int (*fptr_t)(int);
fptr_t is_delim = is_objdump_delim;
/* set up command line arguments */
char *argv[] = { "objdump", "--all-headers", (char *)file, NULL };
if (use_readelf) {
argv[0] = "readelf";
argv[1] = "--dyn-syms";
is_delim = is_readelf_delim;
}
/* open pipe */
FILE *fp = popenvp(argv[0], argv, "re", 1);
if (!fp) {
perror("popenvp()");
return NULL;
}
/* get lines */
while ((c = fgetc(fp)) != EOF) {
/* check for delimiter */
if (!is_delim(c) ||
!fgets(buf, sizeof(buf)-1, fp) ||
strncmp(buf, "GLIBC_", 6) != 0 ||
!isdigit(buf[6]))
{
continue;
}
char *pbuf = buf + 6;
strip_glibc_str(pbuf);
if (*out == 0 || strverscmp(out, pbuf) < 0) {
strcpy(out, pbuf);
}
}
pclose(fp);
return *out ? out : NULL;
}
static int ftw_callback(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
(void)sb;
(void)ftwbuf;
if (typeflag != FTW_F) {
return 0;
}
const char *p = glibc_version(fpath);
if (p && (*glibc == 0 || strverscmp(glibc, p) < 0)) {
strcpy(glibc, p);
strncpy(glibc_file, fpath, sizeof(glibc_file)-1);
}
return 0;
}
int main(int argc, char *argv[])
{
const char *path;
char *rp;
/* parse arguments */
switch (argc) {
case 2:
path = argv[1];
break;
case 3:
if (strcmp(argv[1], "--use-readelf") != 0) {
fprintf(stderr, USAGE_TEXT, argv[0]);
return 1;
}
use_readelf = 1;
path = argv[2];
break;
default:
fprintf(stderr, USAGE_TEXT, argv[0]);
return 1;
}
/* resolve path; ntfw() doesn't follow symlinks */
if ((rp = realpath(path, NULL)) == NULL) {
perror("realpath()");
return 1;
}
/* call objdump(1) / readelf(1) */
if (nftw(rp, ftw_callback, 20, FTW_DEPTH | FTW_PHYS) == -1) {
perror("nftw()");
free(rp);
return 1;
}
/* print result */
if (*glibc) {
printf("%s\nGLIBC_%s\n", glibc_file, glibc);
}
free(rp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment