-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxstack.c
82 lines (68 loc) · 1.58 KB
/
xstack.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
/*
* This program rewrites an ELF header adding execute permission to the stack
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <elf.h>
int
main(int argc, const char *argv[])
{
struct stat statbuf;
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
char *file;
int fd;
int i;
/* Check command line arguments */
if (argc != 2) {
(void) fprintf(stderr, "Usage: %s executable\n", argv[0]);
exit(EXIT_FAILURE);
}
/* Open for reading and writing */
fd = open(argv[1], O_RDWR);
/* Check that worked */
if (fd < 0) {
perror("open()");
exit(EXIT_FAILURE);
}
/* Before we can map it, we need to know how big it is */
if (stat(argv[1], &statbuf) < 0) {
perror("stat()");
exit(EXIT_FAILURE);
}
/*
* Memory map the file. Note we map it MAP_SHARED so changes to be
* written back to the disk.
*/
file = mmap(NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
fd, 0);
/* Check that worked */
if (file == MAP_FAILED) {
perror("mmap()");
exit(EXIT_FAILURE);
}
/* Initialise pointers */
ehdr = (Elf32_Ehdr *)file;
phdr = (Elf32_Phdr *)(file + ehdr->e_phoff);
/* Add execute permission to the stack */
for (i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_GNU_STACK) {
phdr[i].p_flags |= PF_X;
}
}
/* Sync with the disk version */
if (msync((void *)file, statbuf.st_size, MS_SYNC) < 0) {
perror("msync()");
exit(EXIT_FAILURE);
}
/* Unmap */
(void) munmap(file, statbuf.st_size);
/* Close the file */
(void) close(fd);
return (0);
}