1 /*
2 ** pax.c for elfsh
3 **
4 ** Started on  Tue Feb 11 20:52:36 2003 mayhem
5 ** Last update Tue Feb 18 06:17:27 2003 mayhem
6 */
7 #include "libelfsh.h"
8 
9 
10 /* Change the ELF header flags field */
elfsh_set_paxflags(Elf32_Ehdr * hdr,Elf32_Word flags)11 int		elfsh_set_paxflags(Elf32_Ehdr *hdr, Elf32_Word flags)
12 {
13   u_short	*pax_flags;
14 
15   if (!hdr)
16     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
17   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
18   *pax_flags = flags;
19   return (0);
20 }
21 
22 
23 /* Return the ELF header flags field */
elfsh_get_paxflags(Elf32_Ehdr * hdr)24 Elf32_Word	elfsh_get_paxflags(Elf32_Ehdr *hdr)
25 {
26   u_short	*pax_flags;
27 
28   if (!hdr)
29     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
30   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
31   return (*pax_flags);
32 }
33 
34 
35 /* Change the section header table file offset */
elfsh_set_pax_pageexec(Elf32_Ehdr * hdr,u_short off)36 char		elfsh_set_pax_pageexec(Elf32_Ehdr *hdr, u_short off)
37 {
38   u_short	*pax_flags;
39 
40   if (!hdr)
41     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
42   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
43   if (off)
44     *pax_flags |= ELFSH_PAX_PAGEEXEC;
45   else
46     *pax_flags &= (~ELFSH_PAX_PAGEEXEC);
47   return (0);
48 }
49 
50 /* Return the section header table file offset */
elfsh_get_pax_pageexec(Elf32_Ehdr * hdr)51 char		elfsh_get_pax_pageexec(Elf32_Ehdr *hdr)
52 {
53   u_short	*pax_flags;
54 
55   if (!hdr)
56     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
57   pax_flags = (u_short *) ((char *) hdr->e_ident + EI_PAX);
58   return ((*pax_flags & ELFSH_PAX_PAGEEXEC) &&
59 	  !(*pax_flags & ELFSH_PAX_SEGMEXEC));
60 }
61 
62 
63 
64 /* Change the section header table file offset */
elfsh_set_pax_emultramp(Elf32_Ehdr * hdr,u_short off)65 char		elfsh_set_pax_emultramp(Elf32_Ehdr *hdr, u_short off)
66 {
67   u_short	*pax_flags;
68 
69   if (!hdr)
70     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
71   pax_flags = (u_short *) ((char *) hdr->e_ident + EI_PAX);
72   if (off)
73     *pax_flags |= ELFSH_PAX_EMULTRAMP;
74   else
75     *pax_flags &= (~ELFSH_PAX_EMULTRAMP);
76   return (0);
77 }
78 
79 /* Return the section header table file offset */
elfsh_get_pax_emultramp(Elf32_Ehdr * hdr)80 char		elfsh_get_pax_emultramp(Elf32_Ehdr *hdr)
81 {
82   u_short	*pax_flags;
83 
84   if (!hdr)
85     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
86   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
87   return ((*pax_flags & ELFSH_PAX_EMULTRAMP));
88 }
89 
90 /* Change the section header table file offset */
elfsh_set_pax_mprotect(Elf32_Ehdr * hdr,u_short off)91 char		elfsh_set_pax_mprotect(Elf32_Ehdr *hdr, u_short off)
92 {
93   u_short	*pax_flags;
94 
95   if (!hdr)
96     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
97   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
98   if (!off)
99     *pax_flags |= ELFSH_PAX_MPROTECT;
100   else
101     *pax_flags &= (~ELFSH_PAX_MPROTECT);
102   return (0);
103 }
104 
105 /* Return the section header table file offset */
elfsh_get_pax_mprotect(Elf32_Ehdr * hdr)106 char		elfsh_get_pax_mprotect(Elf32_Ehdr *hdr)
107 {
108   u_short	*pax_flags;
109 
110   if (!hdr)
111     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
112   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
113   return (!(*pax_flags & ELFSH_PAX_MPROTECT));
114 }
115 
116 
117 /* Change the section header table file offset */
elfsh_set_pax_randmmap(Elf32_Ehdr * hdr,u_short off)118 char		elfsh_set_pax_randmmap(Elf32_Ehdr *hdr, u_short off)
119 {
120   u_short	*pax_flags;
121 
122   if (!hdr)
123     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
124   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
125   if (!off)
126     *pax_flags |= ELFSH_PAX_RANDMMAP;
127   else
128     *pax_flags &= (~ELFSH_PAX_RANDMMAP);
129   return (0);
130 }
131 
132 /* Return the section header table file offset */
elfsh_get_pax_randmmap(Elf32_Ehdr * hdr)133 char		elfsh_get_pax_randmmap(Elf32_Ehdr *hdr)
134 {
135   u_short	*pax_flags;
136 
137   if (!hdr)
138     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
139   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
140   return (!(*pax_flags & ELFSH_PAX_RANDMMAP));
141 }
142 
143 
144 /* Change the section header table file offset */
elfsh_set_pax_randexec(Elf32_Ehdr * hdr,u_short off)145 char		elfsh_set_pax_randexec(Elf32_Ehdr *hdr, u_short off)
146 {
147   u_short	*pax_flags;
148 
149   if (!hdr)
150     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
151   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
152   if (off)
153     *pax_flags |= ELFSH_PAX_RANDEXEC;
154   else
155     *pax_flags &= (~ELFSH_PAX_RANDEXEC);
156   return (0);
157 }
158 
159 /* Return the section header table file offset */
elfsh_get_pax_randexec(Elf32_Ehdr * hdr)160 char		elfsh_get_pax_randexec(Elf32_Ehdr *hdr)
161 {
162   u_short	*pax_flags;
163 
164   if (!hdr)
165     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
166   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
167   return ((*pax_flags & ELFSH_PAX_RANDEXEC));
168 }
169 
170 
171 /* Change the section header table file offset */
elfsh_set_pax_segmexec(Elf32_Ehdr * hdr,u_short off)172 char		elfsh_set_pax_segmexec(Elf32_Ehdr *hdr, u_short off)
173 {
174   u_short	*pax_flags;
175 
176   if (!hdr)
177     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
178   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
179   if (!off)
180     *pax_flags |= ELFSH_PAX_SEGMEXEC;
181   else
182     *pax_flags &= (~ELFSH_PAX_SEGMEXEC);
183   return (0);
184 }
185 
186 /* Return the section header table file offset */
elfsh_get_pax_segmexec(Elf32_Ehdr * hdr)187 char		elfsh_get_pax_segmexec(Elf32_Ehdr *hdr)
188 {
189   u_short	*pax_flags;
190 
191   if (!hdr)
192     ELFSH_SETERROR("[libelfsh] Invalid NULL parameter", -1);
193   pax_flags   = (u_short *) ((char *) hdr->e_ident + EI_PAX);
194   return (!(*pax_flags & ELFSH_PAX_SEGMEXEC));
195 }
196