Commit 161ad9e4 authored by Vivek Das Mohapatra's avatar Vivek Das Mohapatra

libcapsule is a library used to implement proxying/shim libraries

The generated shim libraries (libz and libGL are included as examples)
provide the same symbols as the target library but do not expose the
symbols from their dependencies to other objects that link to them.

They are also capable of using a filesystem tree starting somwhere
other than / to get the dependencies for the target.

Included are tools to help generate said proxy libraries with minimal
manual intervention.
parents
.deps
Makefile.in
aclocal.m4
autom4te.cache
compile
config.guess
config.sub
configure
depcomp
install-sh
ltmain.sh
m4
m4/*
missing
update
utils
Vivek Das Mohapatra <vivek@collabora.com>; <vivek@etla.org>
This diff is collapsed.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
This diff is collapsed.
# Copyright © 2017 Collabora Ltd
# This file is part of libcapsule.
# libcapsule is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# libcapsule is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
V ?= 0
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = -I m4
AM_CFLAGS = --std=c99 -D_GNU_SOURCE -Og -ggdb -Wall
bin_PROGRAMS = elf-dump print-libstubs
lib_LTLIBRARIES = libcapsule.la libz.la libGL.la
include_HEADERS = capsule/capsule.h
nodist_include_HEADERS = utils/utils.h utils/dump.h utils/process-pt-dynamic.h
elf_dump_SOURCES = utils/elf-dump.c
elf_dump_LDFLAGS = -ldl
print_libstubs_SOURCES = utils/print-libstubs.c
print_libstubs_LDFLAGS = -ldl
libcapsule_la_SOURCES = capsule/capsule-dlmopen.c \
capsule/capsule-relocate.c \
utils/utils.c utils/dump.c \
utils/process-pt-dynamic.c \
utils/mmap-info.c
libcapsule_la_LDFLAGS = -lelf
libz_la_SOURCES = shim/libz.so.c
libz_la_LDFLAGS = -lcapsule -version-info 1:1:0 \
-Wl,--version-script=shim/libz.so.c.map
libGL_la_SOURCES = shim/libGL.so.c
libGL_la_LDFLAGS = -lcapsule -version-info 1:1:0
shims = z GL
shim_srcs = $(foreach l,$(shims),shim/lib$l.so.c)
shim_files = $(foreach y,map symbols,$(foreach x,$(shim_srcs),$x.$y))
GENSTUB_V1 =
GENSTUB_V0 = @echo " GENSTUB " $@.excluded $@.shared $@;
GENSTUB = $(GENSTUB_V$(V))
CLEANFILES = $(shim_files)
BUILT_SOURCES = $(shim_srcs)
shim/%.so.c: shim/%.excluded shim/%.shared
shim/%.so.c: generate-stublib.sh capsule-shim.h print-libstubs
$(GENSTUB)$(top_srcdir)/$< $$(basename $@ .c) $@.excluded $@.shared $@
debug:
@echo srcs : $(shim_srcs)
@echo files: $(shim_files)
#zshim/libz-api.c: generate-shimapi.pl find-include.sh
# @(inc=$$($(top_srcdir)/find-include.sh $(ZLIB_CFLAGS) -izlib.h); \
# gcc -E $$inc | $(top_srcdir)/generate-shimapi.pl) > $@
// Copyright © 2017 Collabora Ltd
// This file is part of libcapsule.
// libcapsule is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// libcapsule is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <capsule/capsule.h>
#define UNVERSIONED_STUB(name) \
void name (void) { fprintf(stderr, "! SHIM " #name " called\n" ); return; }
#define VERSIONED_STUB(name,version) \
UNVERSIONED_STUB(name);
// we used to do the linking here but that actually gets messy
// since you need the link map anyway to declare the version nodes
// and it turns out you have to make an extra _name symbol to do
// versioned symbols via __asm__ directives
// __asm__(".symver _" #name "," #name #version)
This diff is collapsed.
// Copyright © 2017 Collabora Ltd
// This file is part of libcapsule.
// libcapsule is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// libcapsule is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <capsule/capsule.h>
#define DEBUG
#include "utils/dump.h"
#include "utils/utils.h"
#include "utils/process-pt-dynamic.h"
// ==========================================================================
// some entries require us to peer into others to make sense of them:
// can't make full sense of relocations without looking names up
// in the stringtab, which does not have to occur at any fixed point in
// in the PT_DYNAMIC entry.
// IOW PT_DYNAMIC contains both relocations (DT_RELA, DT_REL) and a stringtab
// (DT_STRTAB) in arbitrary order but the former do not make sense without
// the latter.
// =========================================================================
static int
process_phdr (struct dl_phdr_info *info,
size_t size,
relocation_data_t *rdata)
{
int ret = 0;
for( int j = 0; !ret && (j < info->dlpi_phnum); j++ )
if( info->dlpi_phdr[j].p_type == PT_DYNAMIC )
ret = process_pt_dynamic( (void *) info->dlpi_phdr[j].p_vaddr,
info->dlpi_phdr[j].p_memsz,
info->dlpi_addr,
process_dt_rela,
process_dt_rel,
rdata );
return ret;
}
// first level of the callback: all we're doing here is skipping over
// any program headers that (for whatever reason) we decide we're not
// interested in.
// In practice we have to handle all existing DSOs, as any of them may
// call into the library we are acting as a shim for.
static int
relocate_cb (struct dl_phdr_info *info, size_t size, void *data)
{
relocation_data_t *rdata = data;
if( rdata->debug )
debug( "processing %s", *info->dlpi_name ? info->dlpi_name : "-elf-" );
return process_phdr( info, size, rdata );
}
int capsule_relocate (const char *target,
void *source,
int dbg,
capsule_item_t *relocations,
char **error)
{
relocation_data_t rdata = { 0 };
capsule_item_t *map;
int mmap_errno = 0;
char *mmap_error = NULL;
int rval = 0;
rdata.target = target;
rdata.debug = dbg;
rdata.error = NULL;
rdata.relocs = relocations;
rdata.mmap_info = load_mmap_info( &mmap_errno, &mmap_error );
if( dbg && (mmap_errno || mmap_error) )
{
debug("mmap/mprotect flags information load error (errno: %d): %s",
mmap_errno, mmap_error );
debug("relocation will be unable to handle relro linked libraries");
}
// no source dl handle means we must have a pre-populated
// map of shim-to-real function pointers in `relocations',
// otherwise populate the map using dlsym():
if( source )
for( map = relocations; map->name; map++ )
{
if( !map->shim )
map->shim = (ElfW(Addr)) dlsym( RTLD_DEFAULT, map->name );
if( !map->real )
map->real = (ElfW(Addr)) dlsym( source, map->name );
}
// time to enter some sort of ... dangerous... zone:
// we need the mmap()ed DSO regions to have the PROT_WRITE
// flag set, so that if they've been RELRO linked we can still
// overwrite their PLT entries.
for( int i = 0; rdata.mmap_info[i].start != MAP_FAILED; i++ )
if( mmap_entry_should_be_writable( &rdata.mmap_info[i] ) )
add_mmap_protection( &rdata.mmap_info[i], PROT_WRITE );
dl_iterate_phdr( relocate_cb, &rdata );
// and now we put those PROT_WRITE permissions back the way they were:
for( int i = 0; rdata.mmap_info[i].start != MAP_FAILED; i++ )
if( mmap_entry_should_be_writable( &rdata.mmap_info[i] ) )
reset_mmap_protection( &rdata.mmap_info[i] );
if( rdata.error )
{
if( error )
*error = rdata.error;
else
free( rdata.error );
rval = (rdata.count.failure == 0) ? -1 : rdata.count.failure;
}
free_mmap_info( rdata.mmap_info );
rdata.mmap_info = NULL;
return rval;
}
// Copyright © 2017 Collabora Ltd
// This file is part of libcapsule.
// libcapsule is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// libcapsule is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <link.h>
typedef struct
{
const char *name;
ElfW(Addr) shim;
ElfW(Addr) real;
} capsule_item_t;
int capsule_relocate (const char *target,
void *source,
int debug,
capsule_item_t *relocations,
char **error);
void *capsule_dlmopen (const char *dso,
const char *prefix,
Lmid_t *namespace,
capsule_item_t *wrappers,
int debug,
const char **exclude,
int *errcode,
char **error);
void *capsule_shim_dlopen(void *handle,
Lmid_t ns,
const char *prefix,
const char **exclude,
const char *file,
int flag);
dnl Copyright © 2017 Collabora Ltd
dnl This file is part of libcapsule.
dnl libcapsule is free software: you can redistribute it and/or modify
dnl it under the terms of the GNU Lesser General Public License as
dnl published by the Free Software Foundation, either version 3 of the
dnl License, or (at your option) any later version.
dnl libcapsule is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dnl GNU Lesser General Public License for more details.
dnl You should have received a copy of the GNU Lesser General Public
dnl License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
AC_INIT(libcapsule, 0.1)
AC_CONFIG_MACRO_DIR([m4])
dnl don't kvetch about gnu makefile syntax
dnl and don't require ChangeLog &co if they don't exist
AM_INIT_AUTOMAKE([-Wno-portability foreign])
LT_INIT
PKG_CHECK_MODULES([ZLIB], [zlib])
AC_SEARCH_LIBS([dlmopen], [dl])
AC_OUTPUT([Makefile])
#!/bin/bash
# Copyright © 2017 Collabora Ltd
# This file is part of libcapsule.
# libcapsule is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# libcapsule is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
set -u
set -e
declare -A NODE;
top=$(dirname $0);
top=${top:-.};
proxied_dso=$1; shift;
proxy_excluded=$1; shift;
proxy_extra=$1; shift;
proxy_src=$1; shift;
echo -n > $proxy_src.symbols;
echo -n > $proxy_src.map;
exec >& $proxy_src;
cat $top/capsule-shim.h;
for proxied_target in $proxied_dso $(cat $proxy_extra);
do
while read symbol version dependency;
do
case $version in
@*)
echo "VERSIONED_STUB ( $symbol, $version );";
node=${version##*@};
NODE[$node]=${NODE[$node]:-}" "$symbol;
;;
*)
echo "UNVERSIONED_STUB( $symbol );";
;;
esac;
echo " { \"$symbol\" }," >> $proxy_src.symbols
done < <($top/print-libstubs $proxied_target);
done;
cat - <<EOF
static Lmid_t symbol_ns;
static char *prefix;
static void *dso;
// should we exclude libpthread here?
// in any case, these are DSOs we do _not_ want to isolate
static const char *exclude[] = { // MUST NOT be pulled from the capsule prefixed filesystem tree:
"libc.so.6",
"libpthread.so.0",
"libpthread-2.19.so",
"libdl.so.2",
EOF
while read excluded x;
do
case $excluded in lib*) printf "%32s \"%s\",\n" "" $excluded; ;; esac;
done < $proxy_excluded;
cat - <<EOF
NULL };
static void *_dlopen (const char *filename, int flag)
{
return capsule_shim_dlopen( dso, symbol_ns, prefix, exclude, filename, flag );
}
static void __attribute__ ((constructor)) _capsule_init (void)
{
int capsule_errno = 0;
char *capsule_error = NULL;
// this is an array of the functions we want to act as a shim for:
capsule_item_t relocs[] =
{
EOF
cat $proxy_src.symbols;
cat - <<EOF
{ NULL }
};
// and this is an aray of functions we must override in the DSOs
// inside the capsule (mostly to take account of the fact that
// they're pulled in from a tree with a filesystem prefix like /host)
// NOTE: the shim address here isn't used, but we give it the same
// value as the real function address so it's never accidentally
// a value the capsule code will care about:
capsule_item_t wrappers[] =
{
{ "dlopen", (ElfW(Addr)) _dlopen, (ElfW(Addr)) _dlopen },
{ NULL }
};
symbol_ns = LM_ID_NEWLM;
prefix = "/host";
dso = capsule_dlmopen( "$proxied_dso", prefix, &symbol_ns, wrappers,
0, exclude, &capsule_errno, &capsule_error );
if( dso )
{
capsule_relocate( "$proxied_dso", dso, 0, relocs, &capsule_error );
}
else
{
fprintf( stderr, "capsule_dlmopen() failed: %s\\n", capsule_error );
exit( 1 );
}
}
EOF
exec >& $proxy_src.map;
for node in ${!NODE[@]};
do
echo "$node {";
echo " global:";
for symbol in ${NODE[$node]};
do
echo " $symbol;";
done;
echo "};";
echo;
done;