├── LICENSE ├── Makefile ├── README ├── sataniccanary.c └── test.c /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PLUGIN_SOURCE_FILES = sataniccanary.c 2 | PLUGIN_OBJECT_FILES = sataniccanary.o 3 | PLUGIN = sataniccanary.so 4 | 5 | CC = gcc 6 | GCC = gcc 7 | GCCPLUGINS_DIR = $(shell $(GCC) -print-file-name=plugin) 8 | CFLAGS += -I$(GCCPLUGINS_DIR)/include -fPIC -g3 -O0 \ 9 | -Wall -pedantic -std=c99 $(EXTRA_CFLAGS) 10 | 11 | $(PLUGIN): $(PLUGIN_OBJECT_FILES) 12 | $(GCC) -g -shared $^ -o $@ $(CFLAGS) 13 | 14 | test: clean $(PLUGIN) test.c 15 | $(GCC) test.c -o $@ -fplugin=./$(PLUGIN) \ 16 | -g3 -O0 $(EXTRA_ARGS) 17 | 18 | clean: 19 | rm -fv $(PLUGIN) *.o test 20 | 21 | # Some stuff I use for debugging 22 | #debug: 23 | # exec gdb --args /home/enferex/docs/edu/go/dev/gcc-obj/gcc/cc1 \ 24 | # -fplugin=./$(PLUGIN) test.c 25 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Satanic Canary: A GCC plugin implementing various stack canaries. 2 | ================================================================= 3 | 4 | Summary 5 | ------- 6 | The Satanic Canary gcc plugin implements three types of stack canaries. 7 | Two of these are currently enabled, and they are described below. 8 | 9 | This plugin is merely for testing/exploring stack canaries and what they can 10 | do for binary runtime security. I feel safe in saying that the Basic and 11 | TSC Data canaries can be used, but are not perfect. Canaries are not always 12 | impervious to compromise. Likewise, they can impart overhead to the program 13 | being executed. 14 | 15 | A canary, or stack cookie, is merely a value on the stack which is placed 16 | there at compile time in the function prologue. During runtime, at function 17 | epilogue, the sanity of that value is checked. If the value has been 18 | modified then the canary calls an abort() since the stack has been corrupted 19 | (either through bad programming or a malicious intent). 20 | 21 | The canaries are chosen at 'random' for each function being compiled. 22 | 23 | The array of structs below in the 'canaries' array are the canaries that can 24 | be enabled/disabled. 25 | 26 | Canaries Provided by this Plugin 27 | -------------------------------- 28 | * Basic Canary: This canary places a random constant value/canary on the 29 | stack and this same value should lie there unmodified upon function 30 | return/epilogue. This value will be different for each function compiled. 31 | 32 | * TSC Canary: This canary places a value on the stack. This value is 33 | obtained from the more active (low 32bits) of the Timestamp Counter (TSC). 34 | The TSC is dynamic and different for each execution of the function at 35 | runtime. This is a really craptastic canary and should not be used. It is 36 | easily compromised if the stack if overrun with the same data. For each call 37 | to a function with this canary enabled, the TSC value is placed twice on the 38 | stack, back to back. If, upon prologue, the two values differ, then the 39 | stack is said to be corrupt and the program abort()s out. Now, if the stack 40 | is overwritten with the same value/pattern, then, these two values will still 41 | be the same, e.g. and the canary will not detect a corruption... bad, bad 42 | canary! 43 | 44 | * TSC Data Canary: This canary places a TSC stamp, the low 32-bits, on the 45 | stack and XOR's it against read-only data in the CS segment. That XOR value, 46 | (DATA xor TSC) is placed on the stack also. Upon function epilogue, we 47 | verify the stack sanity by XOR'ing the CS data and the (TSC xor DATA) that 48 | was pushed onto the stack. The result should be the TSC value we pushed on 49 | the stack as the first value. 50 | 51 | Contact 52 | ------- 53 | Matt Davis (enferex) 54 | mattdavis9@gmail.com 55 | -------------------------------------------------------------------------------- /sataniccanary.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * sataniccanary.c 3 | * 4 | * Satanic Canary: A GCC plugin implementing various stack canaries. 5 | * 6 | * The Satanic Canary gcc plugin implements three types of stack canaries. 7 | * Two of these are currently enabled, and they are described below. 8 | * 9 | * This plugin is merely for testing/exploring stack canaries and what they can 10 | * do for binary runtime security. I feel safe in saying that the Basic and 11 | * TSC Data canaries can be used, but are not perfect. Canaries are not always 12 | * impervious to compromise. Likewise, they can impart overhead to the program 13 | * being executed. 14 | * 15 | * A canary, or stack cookie, is merely a value on the stack which is placed 16 | * there at compile time in the function prologue. During runtime, at function 17 | * epilogue, the sanity of that value is checked. If the value has been 18 | * modified then the canary calls an abort() since the stack has been corrupted 19 | * (either through bad programming or a malicious intent). 20 | * 21 | * The canaries are chosen at 'random' for each function being compiled. 22 | * 23 | * The array of structs below in the 'canaries' array are the canaries that can 24 | * be enabled/disabled: 25 | * 26 | * -- Basic Canary: This canary places a random constant value/canary on the 27 | * stack and this same value should lie there unmodified upon function 28 | * return/epilogue. This value will be different for each function compiled. 29 | * 30 | * -- TSC Canary: This canary places a value on the stack. This value is 31 | * obtained from the more active (low 32bits) of the Timestamp Counter (TSC). 32 | * The TSC is dynamic and different for each execution of the function at 33 | * runtime. This is a really craptastic canary and should not be used. It is 34 | * easily compromised if the stack if overrun with the same data. For each call 35 | * to a function with this canary enabled, the TSC value is placed twice on the 36 | * stack, back to back. If, upon prologue, the two values differ, then the 37 | * stack is said to be corrupt and the program abort()s out. Now, if the stack 38 | * is overwritten with the same value/pattern, then, these two values will still 39 | * be the same, e.g. and the canary will not detect a corruption... bad, bad 40 | * canary! 41 | * 42 | * -- TSC Data Canary: This canary places a TSC stamp, the low 32-bits, on the 43 | * stack and XOR's it against read-only data in the CS segment. That XOR value, 44 | * (DATA xor TSC) is placed on the stack also. Upon function epilogue, we 45 | * verify the stack sanity by XOR'ing the CS data and the (TSC xor DATA) that 46 | * was pushed onto the stack. The result should be the TSC value we pushed on 47 | * the stack as the first value. 48 | * 49 | * Copyright (C) 2012 Matt Davis (enferex) 50 | * 51 | * This program is free software; you can redistribute it and/or modify it under 52 | * the terms of the GNU General Public License as published by the Free Software 53 | * Foundation; either version 2 of the License, or (at your option) any later 54 | * version. 55 | * 56 | * This program is distributed in the hope that it will be useful, but WITHOUT 57 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 58 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 59 | * details. 60 | * 61 | * You should have received a copy of the GNU General Public License along with 62 | * this program; if not, see 63 | ******************************************************************************/ 64 | 65 | #include 66 | #include 67 | #include 68 | #include 69 | #include 70 | #include 71 | #include 72 | #include 73 | 74 | 75 | /* Printing and debugging aids */ 76 | #define P(...) {printf("[+] " __VA_ARGS__); printf("\n");} 77 | #ifdef DEBUG 78 | #define D(...) P(__VA_ARGS__) 79 | #else 80 | #define D(...) /* Nothing */ 81 | #endif 82 | 83 | 84 | /* Bendover */ 85 | int plugin_is_GPL_compatible = 0; 86 | 87 | 88 | /* For the plugin. This will try to canary-fy each function (to make the 89 | * functions have a canary). 90 | */ 91 | static bool sataniccanary_gate(void) 92 | { 93 | return true; 94 | } 95 | 96 | 97 | /* Forward decls for our canary methods. Each canary method has a before and 98 | * after call. The 'setup' is passed the function-to-be-modified's 99 | * PROLOGUE_END insn NOTE. And the 'finish' is passed the 100 | * function-to-be-modified's EPILOGUE_BEG insn NOTE. Ideally in 'setup' the 101 | * canary is placed in the stack. And the 'finish' is used to verify that the 102 | * canary has not been modified (e.g. has not keeled over and died from toxic 103 | * fumes. 104 | */ 105 | static void setup_basic_canary(rtx insn); 106 | static void finish_basic_canary(rtx insn); 107 | static void setup_tsc_canary(rtx insn); 108 | static void finish_tsc_canary(rtx insn); 109 | static void setup_tscdata_canary(rtx insn); 110 | static void finish_tscdata_canary(rtx insn); 111 | typedef void(*canary)(rtx); 112 | struct {canary setup; canary finish;} canaries[] = 113 | { 114 | {setup_basic_canary, finish_basic_canary}, 115 | /* {setup_tsc_canary, finish_tsc_canary}, <--- DO NOT ENABLE THIS SUCKS */ 116 | {setup_tscdata_canary, finish_tscdata_canary}, 117 | }; 118 | 119 | 120 | /* Insert a static (compile-time known) canary value, it will change once per 121 | * each time this function is called. The value comes from rand() but might 122 | * want to consider using /dev/urandom 123 | */ 124 | static int rng_guard_value; 125 | static void setup_basic_canary(rtx insn) 126 | { 127 | rtx dec, mov, psh, mem; 128 | 129 | /* Each pair (setup and check) will use this value, it will change each time 130 | * a new "basic canary" is inserted 131 | */ 132 | rng_guard_value = rand(); 133 | 134 | /* mov $, %rax */ 135 | mov = gen_rtx_SET(DImode, 136 | gen_rtx_REG(DImode, 0), 137 | gen_rtx_CONST_INT(VOIDmode, rng_guard_value)); 138 | 139 | /* push %rax */ 140 | dec = gen_rtx_PRE_DEC(DImode, stack_pointer_rtx); 141 | mem = gen_rtx_MEM(DImode, dec); 142 | psh = gen_rtx_SET(DImode, mem, gen_rtx_REG(DImode, 0)); 143 | 144 | emit_insn_before(mov, insn); 145 | emit_insn_before(psh, insn); 146 | } 147 | 148 | 149 | /* Check that the static canary value has not been stepped on */ 150 | static void finish_basic_canary(rtx insn) 151 | { 152 | /* Get the canary from stack and put it into %rax */ 153 | rtx last, mem, rbx, tmp, label; 154 | 155 | /* pop %rbx */ 156 | rbx = gen_rtx_POST_INC(DImode, stack_pointer_rtx); 157 | mem = gen_rtx_MEM(DImode, rbx); 158 | tmp = gen_rtx_SET(DImode, gen_rtx_REG(DImode, 1), mem); 159 | last = emit_insn_after(tmp, insn); 160 | 161 | /* cmp $666, %rbx */ 162 | tmp = gen_rtx_COMPARE(CCmode, 163 | gen_rtx_REG(DImode, 1), 164 | gen_rtx_CONST_INT(VOIDmode, rng_guard_value)); 165 | tmp = gen_rtx_SET(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), tmp); 166 | last = emit_insn_after(tmp, last); 167 | 168 | /* jeq */ 169 | label = gen_label_rtx(); /* Where we jump to */ 170 | tmp = gen_rtx_EQ(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), const0_rtx); 171 | tmp = gen_rtx_IF_THEN_ELSE(VOIDmode, 172 | tmp, /* cmp */ 173 | gen_rtx_LABEL_REF(VOIDmode, label), /* Ifeq */ 174 | pc_rtx); /* Else (do nothing) */ 175 | last = emit_jump_insn_after(gen_rtx_SET(VOIDmode, pc_rtx, tmp), last); 176 | JUMP_LABEL(last) = label; 177 | 178 | /* Call abort() */ 179 | tmp = gen_rtx_SYMBOL_REF(Pmode, "abort"); 180 | tmp = gen_rtx_CALL(Pmode, gen_rtx_MEM(QImode, tmp), const0_rtx); 181 | last = emit_insn_after(tmp, last); 182 | emit_label_after(label, last); 183 | } 184 | 185 | 186 | /* Set the TSC value to the canary value. Push this value onto the stack twice. 187 | * The check will pop both values and compare. 188 | */ 189 | static void setup_tsc_canary(rtx insn) 190 | { 191 | rtx tsc, psh; 192 | rtvec av, cv, lv; 193 | 194 | av = rtvec_alloc(0); 195 | cv = rtvec_alloc(0); 196 | lv = rtvec_alloc(0); 197 | 198 | /* rdtsc */ 199 | tsc = gen_rtx_ASM_OPERANDS(VOIDmode, "rdtsc", "", 0, av, cv, lv, 200 | expand_location(RTL_LOCATION(insn)).line); 201 | emit_insn_before(tsc, insn); 202 | 203 | /* Push the low end (rax) result of rdtsc. we figure it has more random 204 | * values than the higher bits of the rdtsc result. It is a cycle counter, 205 | * so the lower bits are more 'fresh' than the higher bits. We push it 206 | * twice so that we can compare the two canary values later. 207 | */ 208 | psh = gen_rtx_PRE_DEC(DImode, stack_pointer_rtx); 209 | psh = gen_rtx_MEM(DImode, psh); 210 | psh = gen_rtx_SET(DImode, psh, gen_rtx_REG(DImode, 0)); 211 | emit_insn_before(psh, insn); 212 | emit_insn_before(psh, insn); 213 | } 214 | 215 | 216 | /* Pop the two tsc values from the stack, and compare. The same tsc value was 217 | * push'd twice, therefore the two values should match. 218 | */ 219 | static void finish_tsc_canary(rtx insn) 220 | { 221 | rtx pop, mem, cmp, jmp, label, last, call; 222 | 223 | /* pop %rbx, pop %rcx (rax has the return value) */ 224 | mem = gen_rtx_POST_INC(DImode, stack_pointer_rtx); 225 | mem = gen_rtx_MEM(DImode, mem); 226 | pop = gen_rtx_SET(DImode, gen_rtx_REG(DImode, 1), mem); 227 | last = emit_insn_after(pop, insn); 228 | pop = gen_rtx_SET(DImode, gen_rtx_REG(DImode, 2), mem); 229 | last = emit_insn_after(pop, last); 230 | 231 | /* cmp %rbx, %rcx */ 232 | cmp = gen_rtx_COMPARE(CCmode, 233 | gen_rtx_REG(DImode, 1), 234 | gen_rtx_REG(DImode, 2)); 235 | cmp = gen_rtx_SET(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), cmp); 236 | last = emit_insn_after(cmp, last); 237 | 238 | /* jeq */ 239 | label = gen_label_rtx(); 240 | jmp = gen_rtx_EQ(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), const0_rtx); 241 | jmp = gen_rtx_IF_THEN_ELSE(VOIDmode, 242 | jmp, 243 | gen_rtx_LABEL_REF(VOIDmode, label), 244 | pc_rtx); 245 | jmp = gen_rtx_SET(VOIDmode, pc_rtx, jmp); 246 | last = emit_jump_insn_after(jmp, last); 247 | JUMP_LABEL(last) = label; 248 | 249 | /* Call abort() */ 250 | call = gen_rtx_SYMBOL_REF(Pmode, "abort"); 251 | call = gen_rtx_CALL(Pmode, gen_rtx_MEM(QImode, call), const0_rtx); 252 | last = emit_insn_after(call, last); 253 | emit_label_after(label, last); 254 | } 255 | 256 | 257 | /* Push TSC and (TSC xor DATA) value onto the stack. We verify data by XOR'ing 258 | * DATA and the (TSC xor DATA) that was pushed onto the stack. 259 | */ 260 | static void setup_tscdata_canary(rtx insn) 261 | { 262 | rtx tsc, psh, and, xor, eax, ebx, ecx, tmp; 263 | rtvec av, cv, lv; 264 | 265 | av = rtvec_alloc(0); 266 | cv = rtvec_alloc(0); 267 | lv = rtvec_alloc(0); 268 | 269 | /* Registers (for convenience) */ 270 | eax = gen_rtx_REG(DImode, AX_REG); 271 | ebx = gen_rtx_REG(DImode, BX_REG); 272 | ecx = gen_rtx_REG(DImode, CX_REG); 273 | 274 | /* rdtsc */ 275 | tsc = gen_rtx_ASM_OPERANDS(VOIDmode, "rdtsc", "", 0, av, cv, lv, 276 | expand_location(RTL_LOCATION(insn)).line); 277 | emit_insn_before(tsc, insn); 278 | 279 | /* push low 32bits of rdtsc */ 280 | psh = gen_rtx_PRE_DEC(DImode, stack_pointer_rtx); 281 | psh = gen_rtx_MEM(DImode, psh); 282 | psh = gen_rtx_SET(DImode, psh, eax); 283 | emit_insn_before(psh, insn); 284 | 285 | /* Treat the low 32bits of rdtsc as a random value. We will mask out all 286 | * but 8 bits of it. The low 8 bits we then use, like a random value, as an 287 | * index into the code segment. We take codesegment + (low-32bits & 288 | * 0x000F). This value will be our "DATA" value we xor against the TSC and 289 | * slap onto the stack. A TSC+DATA canary. 290 | * 291 | * To simulate a "random" data address to use, we use the low 8 bits of the 292 | * TSC in EAX. But we still need full eax 32bits, so copy eax into ebx. 293 | */ 294 | tmp = gen_rtx_SET(DImode, ebx, eax); 295 | emit_insn_before(tmp, insn); 296 | and = gen_anddi3(ebx, ebx, GEN_INT(0x000F)); 297 | emit_insn_before(and, insn); 298 | 299 | /* Now get some data from the readonly data segment 300 | * mov %CS:$rbx, %rcx 301 | */ 302 | tmp = gen_rtx_ASM_OPERANDS(VOIDmode, "mov %%cs, %%rcx", "", 0, av, cv, lv, 303 | expand_location(RTL_LOCATION(insn)).line); 304 | emit_insn_before(tmp, insn); 305 | tmp = gen_rtx_SET(DImode, ecx, gen_rtx_PLUS(DImode, ecx, ebx)); 306 | emit_insn_before(tmp, insn); 307 | 308 | /* xor %rcx, %rax */ 309 | xor = gen_xordi3(ecx, ecx, eax); 310 | emit_insn_before(xor, insn); 311 | 312 | /* push %rcx */ 313 | psh = gen_rtx_PRE_DEC(DImode, stack_pointer_rtx); 314 | psh = gen_rtx_MEM(DImode, psh); 315 | psh = gen_rtx_SET(DImode, psh, ecx); 316 | emit_insn_before(psh, insn); 317 | } 318 | 319 | 320 | /* XOR the TSC value on the stack and the (TSC xor DATA) value on the stack. 321 | * The xor'ing of these two values should equal the spot in data. If not, the 322 | * stack has been stepped on. 323 | */ 324 | static void finish_tscdata_canary(rtx insn) 325 | { 326 | rtx and, psh, pop, mem, eax, ebx, ecx, edx; 327 | rtx last, xor, cmp, jmp, label, call; 328 | rtvec av, cv, lv; 329 | 330 | /* Convenience */ 331 | eax = gen_rtx_REG(DImode, AX_REG); 332 | ebx = gen_rtx_REG(DImode, BX_REG); 333 | ecx = gen_rtx_REG(DImode, CX_REG); 334 | edx = gen_rtx_REG(DImode, DX_REG); 335 | av = rtvec_alloc(0); 336 | cv = rtvec_alloc(0); 337 | lv = rtvec_alloc(0); 338 | 339 | /* Now pop the values off the stack (TSC xor DATA) and then TSC */ 340 | mem = gen_rtx_POST_INC(DImode, stack_pointer_rtx); 341 | mem = gen_rtx_MEM(DImode, mem); 342 | pop = gen_rtx_SET(DImode, edx, mem); /* TSC xor DATA */ 343 | last = emit_insn_after(pop, insn); 344 | pop = gen_rtx_SET(DImode, ebx, mem); /* TSC */ 345 | last = emit_insn_after(pop, last); 346 | 347 | /* Push eax so we can save the return value (we need the register) */ 348 | psh = gen_rtx_PRE_DEC(DImode, stack_pointer_rtx); 349 | psh = gen_rtx_MEM(DImode, psh); 350 | psh = gen_rtx_SET(DImode, psh, eax); 351 | last = emit_insn_after(psh, last); 352 | 353 | /* Now get the DATA value CS:(tsc-based-offset) and put it in ecx. 354 | * Remember we use the low-32bits of the TSC (which was stored on the stack 355 | * at function prologue). We mask all but the last 8 bits and use the 356 | * resulting value as an offset to the code segment. This value is called 357 | * the DATA which we xor against the TSC. 358 | */ 359 | mem = gen_rtx_SET(DImode, eax, ebx); /* mov %ebx, $eax */ 360 | last = emit_insn_after(mem, last); 361 | and = gen_anddi3(eax, eax, GEN_INT(0x000F)); /* and %eax, $0x000f */ 362 | last = emit_insn_after(and, last); 363 | mem = gen_rtx_ASM_OPERANDS(VOIDmode, "mov %%cs, %%rcx", "", 0, av, cv, lv, 364 | expand_location(RTL_LOCATION(insn)).line); 365 | last = emit_insn_after(mem, last); 366 | mem = gen_rtx_SET(DImode, ecx, gen_rtx_PLUS(DImode, ecx, eax)); 367 | last = emit_insn_after(mem, last); /* add %eax, %ecx */ 368 | 369 | /* Restore the return value (put 'er back into rax) */ 370 | mem = gen_rtx_POST_INC(DImode, stack_pointer_rtx); 371 | mem = gen_rtx_MEM(DImode, mem); 372 | pop = gen_rtx_SET(DImode, eax, mem); /* Return value */ 373 | last = emit_insn_after(pop, last); 374 | 375 | /* Now xor (TSC xor DATA) and DATA */ 376 | xor = gen_xordi3(ecx, ecx, edx); 377 | last = emit_insn_after(xor, last); 378 | 379 | /* Now compare the xor'd value (ecx) and the originally push'd low 32bits of 380 | * the TSC (ebx) (they should match) 381 | * cmp %rcx, %rbx 382 | */ 383 | cmp = gen_rtx_COMPARE(CCmode, ecx, ebx); 384 | cmp = gen_rtx_SET(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), cmp); 385 | last = emit_insn_after(cmp, last); 386 | 387 | /* jeq */ 388 | label = gen_label_rtx(); 389 | jmp = gen_rtx_EQ(VOIDmode, gen_rtx_REG(CCmode, FLAGS_REG), const0_rtx); 390 | jmp = gen_rtx_IF_THEN_ELSE( 391 | VOIDmode, jmp, gen_rtx_LABEL_REF(VOIDmode, label), pc_rtx); 392 | jmp = gen_rtx_SET(VOIDmode, pc_rtx, jmp); 393 | last = emit_jump_insn_after(jmp, last); 394 | JUMP_LABEL(last) = label; 395 | 396 | /* Call abort() */ 397 | call = gen_rtx_SYMBOL_REF(Pmode, "abort"); 398 | call = gen_rtx_CALL(Pmode, gen_rtx_MEM(QImode, call), const0_rtx); 399 | last = emit_insn_after(call, last); 400 | emit_label_after(label, last); 401 | } 402 | 403 | 404 | static unsigned sataniccanary_exec(void) 405 | { 406 | int idx; 407 | rtx insn; 408 | 409 | P("Adding canary to: %s", get_name(cfun->decl)); 410 | 411 | /* Choose a canary method to use */ 412 | idx = rand() % sizeof(canaries)/sizeof(canaries[0]); 413 | 414 | /* For each instruction in this function */ 415 | for (insn=get_insns(); insn; insn=NEXT_INSN(insn)) 416 | if (NOTE_P(insn) && (NOTE_KIND(insn) == NOTE_INSN_PROLOGUE_END)) 417 | canaries[idx].setup(insn); 418 | else if (NOTE_P(insn) && NOTE_KIND(insn) == NOTE_INSN_EPILOGUE_BEG) 419 | canaries[idx].finish(insn); 420 | 421 | #ifdef DEBUG 422 | print_rtl(stdout, get_insns()); 423 | #endif 424 | 425 | return 0; 426 | } 427 | 428 | 429 | static struct rtl_opt_pass sataniccanary = 430 | { 431 | .pass.type = RTL_PASS, 432 | .pass.name = "sataniccanary", 433 | .pass.gate = sataniccanary_gate, 434 | .pass.execute = sataniccanary_exec, 435 | .pass.todo_flags_finish = TODO_dump_func, 436 | }; 437 | 438 | 439 | /* Return 0 on success or error code on failure */ 440 | int plugin_init( 441 | struct plugin_name_args *info, /* Argument info */ 442 | struct plugin_gcc_version *ver) /* Version of GCC */ 443 | { 444 | struct register_pass_info pass = 445 | { 446 | .pass = &sataniccanary.pass, 447 | .reference_pass_name = "pro_and_epilogue", 448 | .ref_pass_instance_number = 0, 449 | .pos_op = PASS_POS_INSERT_AFTER, 450 | }; 451 | 452 | /* Some canaries (basic canary) call rand() */ 453 | srand(time(NULL)); 454 | 455 | register_callback("sataniccanary", PLUGIN_PASS_MANAGER_SETUP, NULL, &pass); 456 | return 0; 457 | } 458 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static void horribly_unsafe_function(void) 6 | { 7 | int i; 8 | char buf[128]; 9 | 10 | for (i=0; i<1024; ++i) 11 | buf[i] = 0xff; 12 | } 13 | 14 | 15 | int main(int argc, char **argv) 16 | { 17 | horribly_unsafe_function(); 18 | return 0; 19 | } 20 | --------------------------------------------------------------------------------