2011-02-12 00:22:29 -05:00
|
|
|
#ifndef LIBC_H
|
|
|
|
#define LIBC_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-02-20 22:30:06 -05:00
|
|
|
struct __libc {
|
2011-08-06 20:20:23 -04:00
|
|
|
void *main_thread;
|
2011-04-17 16:53:54 -04:00
|
|
|
int threaded;
|
|
|
|
int canceldisable;
|
2011-02-12 00:22:29 -05:00
|
|
|
int (*atexit)(void (*)(void));
|
|
|
|
void (*fini)(void);
|
|
|
|
void (*ldso_fini)(void);
|
|
|
|
volatile int threads_minus_1;
|
2011-04-01 22:35:20 -04:00
|
|
|
int ofl_lock;
|
2011-03-12 21:55:45 -05:00
|
|
|
FILE *ofl_head;
|
2011-02-20 22:30:06 -05:00
|
|
|
};
|
|
|
|
|
2011-02-24 16:37:21 -05:00
|
|
|
|
|
|
|
#if 100*__GNUC__+__GNUC_MINOR__ >= 303 || defined(__PCC__) || defined(__TINYC__)
|
|
|
|
extern struct __libc __libc __attribute__((visibility("hidden")));
|
|
|
|
#define libc __libc
|
|
|
|
|
|
|
|
#elif !defined(__PIC__)
|
2011-02-20 22:30:06 -05:00
|
|
|
extern struct __libc __libc;
|
|
|
|
#define libc __libc
|
2011-02-24 16:37:21 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
#define USE_LIBC_ACCESSOR
|
|
|
|
extern struct __libc *__libc_loc(void) __attribute__((const));
|
|
|
|
#define libc (*__libc_loc())
|
|
|
|
|
2011-02-20 22:30:06 -05:00
|
|
|
#endif
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* Designed to avoid any overhead in non-threaded processes */
|
|
|
|
void __lock(volatile int *);
|
2011-07-30 08:02:14 -04:00
|
|
|
int __lockfile(FILE *);
|
|
|
|
void __unlockfile(FILE *);
|
2011-02-12 00:22:29 -05:00
|
|
|
#define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1))
|
2011-04-17 16:53:54 -04:00
|
|
|
#define UNLOCK(x) (*(volatile int *)(x)=0)
|
2011-02-12 00:22:29 -05:00
|
|
|
|
new attempt at making set*id() safe and robust
changing credentials in a multi-threaded program is extremely
difficult on linux because it requires synchronizing the change
between all threads, which have their own thread-local credentials on
the kernel side. this is further complicated by the fact that changing
the real uid can fail due to exceeding RLIMIT_NPROC, making it
possible that the syscall will succeed in some threads but fail in
others.
the old __rsyscall approach being replaced was robust in that it would
report failure if any one thread failed, but in this case, the program
would be left in an inconsistent state where individual threads might
have different uid. (this was not as bad as glibc, which would
sometimes even fail to report the failure entirely!)
the new approach being committed refuses to change real user id when
it cannot temporarily set the rlimit to infinity. this is completely
POSIX conformant since POSIX does not require an implementation to
allow real-user-id changes for non-privileged processes whatsoever.
still, setting the real uid can fail due to memory allocation in the
kernel, but this can only happen if there is not already a cached
object for the target user. thus, we forcibly serialize the syscalls
attempts, and fail the entire operation on the first failure. this
*should* lead to an all-or-nothing success/failure result, but it's
still fragile and highly dependent on kernel developers not breaking
things worse than they're already broken.
ideally linux will eventually add a CLONE_USERCRED flag that would
give POSIX conformant credential changes without any hacks from
userspace, and all of this code would become redundant and could be
removed ~10 years down the line when everyone has abandoned the old
broken kernels. i'm not holding my breath...
2011-07-29 22:59:44 -04:00
|
|
|
void __synccall(void (*)(void *), void *);
|
2011-08-12 10:32:22 -04:00
|
|
|
void __synccall_wait(void);
|
new attempt at making set*id() safe and robust
changing credentials in a multi-threaded program is extremely
difficult on linux because it requires synchronizing the change
between all threads, which have their own thread-local credentials on
the kernel side. this is further complicated by the fact that changing
the real uid can fail due to exceeding RLIMIT_NPROC, making it
possible that the syscall will succeed in some threads but fail in
others.
the old __rsyscall approach being replaced was robust in that it would
report failure if any one thread failed, but in this case, the program
would be left in an inconsistent state where individual threads might
have different uid. (this was not as bad as glibc, which would
sometimes even fail to report the failure entirely!)
the new approach being committed refuses to change real user id when
it cannot temporarily set the rlimit to infinity. this is completely
POSIX conformant since POSIX does not require an implementation to
allow real-user-id changes for non-privileged processes whatsoever.
still, setting the real uid can fail due to memory allocation in the
kernel, but this can only happen if there is not already a cached
object for the target user. thus, we forcibly serialize the syscalls
attempts, and fail the entire operation on the first failure. this
*should* lead to an all-or-nothing success/failure result, but it's
still fragile and highly dependent on kernel developers not breaking
things worse than they're already broken.
ideally linux will eventually add a CLONE_USERCRED flag that would
give POSIX conformant credential changes without any hacks from
userspace, and all of this code would become redundant and could be
removed ~10 years down the line when everyone has abandoned the old
broken kernels. i'm not holding my breath...
2011-07-29 22:59:44 -04:00
|
|
|
int __setxid(int, int, int, int);
|
2011-04-06 20:27:07 -04:00
|
|
|
|
2011-02-12 00:22:29 -05:00
|
|
|
extern char **__environ;
|
|
|
|
#define environ __environ
|
|
|
|
|
|
|
|
#undef weak_alias
|
|
|
|
#define weak_alias(old, new) \
|
|
|
|
extern __typeof(old) new __attribute__((weak, alias(#old)))
|
|
|
|
|
|
|
|
#undef LFS64_2
|
|
|
|
//#define LFS64_2(x, y) weak_alias(x, y)
|
|
|
|
#define LFS64_2(x, y) extern __typeof(x) y
|
|
|
|
|
|
|
|
#undef LFS64
|
|
|
|
#define LFS64(x) LFS64_2(x, x##64)
|
|
|
|
|
|
|
|
#endif
|