Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 29 | #include <private/bionic_asm.h> |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 30 | |
| 31 | .syntax unified |
| 32 | |
| 33 | .thumb |
| 34 | .thumb_func |
| 35 | |
| 36 | // Get the length of the source string first, then do a memcpy of the data |
| 37 | // instead of a strcpy. |
| 38 | ENTRY(__strcpy_chk) |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 39 | pld [r0, #0] |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 40 | push {r0, lr} |
Brent DeGraaf | 1d0268c | 2013-10-02 13:47:11 +0000 | [diff] [blame] | 41 | .cfi_adjust_cfa_offset 8 |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 42 | .cfi_rel_offset r0, 0 |
| 43 | .cfi_rel_offset lr, 4 |
| 44 | |
| 45 | mov lr, r2 |
| 46 | mov r0, r1 |
| 47 | |
| 48 | ands r3, r1, #7 |
| 49 | beq .L_mainloop |
| 50 | |
| 51 | // Align to a double word (64 bits). |
| 52 | rsb r3, r3, #8 |
| 53 | lsls ip, r3, #31 |
| 54 | beq .L_align_to_32 |
| 55 | |
| 56 | ldrb r2, [r0], #1 |
| 57 | cbz r2, .L_update_count_and_finish |
| 58 | |
| 59 | .L_align_to_32: |
| 60 | bcc .L_align_to_64 |
| 61 | ands ip, r3, #2 |
| 62 | beq .L_align_to_64 |
| 63 | |
| 64 | ldrb r2, [r0], #1 |
| 65 | cbz r2, .L_update_count_and_finish |
| 66 | ldrb r2, [r0], #1 |
| 67 | cbz r2, .L_update_count_and_finish |
| 68 | |
| 69 | .L_align_to_64: |
| 70 | tst r3, #4 |
| 71 | beq .L_mainloop |
| 72 | ldr r3, [r0], #4 |
| 73 | |
| 74 | sub ip, r3, #0x01010101 |
| 75 | bic ip, ip, r3 |
| 76 | ands ip, ip, #0x80808080 |
| 77 | bne .L_zero_in_second_register |
| 78 | |
| 79 | .p2align 2 |
| 80 | .L_mainloop: |
| 81 | ldrd r2, r3, [r0], #8 |
| 82 | |
| 83 | pld [r0, #64] |
| 84 | |
| 85 | sub ip, r2, #0x01010101 |
| 86 | bic ip, ip, r2 |
| 87 | ands ip, ip, #0x80808080 |
| 88 | bne .L_zero_in_first_register |
| 89 | |
| 90 | sub ip, r3, #0x01010101 |
| 91 | bic ip, ip, r3 |
| 92 | ands ip, ip, #0x80808080 |
| 93 | bne .L_zero_in_second_register |
| 94 | b .L_mainloop |
| 95 | |
| 96 | .L_update_count_and_finish: |
| 97 | sub r3, r0, r1 |
| 98 | sub r3, r3, #1 |
| 99 | b .L_check_size |
| 100 | |
| 101 | .L_zero_in_first_register: |
| 102 | sub r3, r0, r1 |
| 103 | lsls r2, ip, #17 |
| 104 | bne .L_sub8_and_finish |
| 105 | bcs .L_sub7_and_finish |
| 106 | lsls ip, ip, #1 |
| 107 | bne .L_sub6_and_finish |
| 108 | |
| 109 | sub r3, r3, #5 |
| 110 | b .L_check_size |
| 111 | |
| 112 | .L_sub8_and_finish: |
| 113 | sub r3, r3, #8 |
| 114 | b .L_check_size |
| 115 | |
| 116 | .L_sub7_and_finish: |
| 117 | sub r3, r3, #7 |
| 118 | b .L_check_size |
| 119 | |
| 120 | .L_sub6_and_finish: |
| 121 | sub r3, r3, #6 |
| 122 | b .L_check_size |
| 123 | |
| 124 | .L_zero_in_second_register: |
| 125 | sub r3, r0, r1 |
| 126 | lsls r2, ip, #17 |
| 127 | bne .L_sub4_and_finish |
| 128 | bcs .L_sub3_and_finish |
| 129 | lsls ip, ip, #1 |
| 130 | bne .L_sub2_and_finish |
| 131 | |
| 132 | sub r3, r3, #1 |
| 133 | b .L_check_size |
| 134 | |
| 135 | .L_sub4_and_finish: |
| 136 | sub r3, r3, #4 |
| 137 | b .L_check_size |
| 138 | |
| 139 | .L_sub3_and_finish: |
| 140 | sub r3, r3, #3 |
| 141 | b .L_check_size |
| 142 | |
| 143 | .L_sub2_and_finish: |
| 144 | sub r3, r3, #2 |
| 145 | |
| 146 | .L_check_size: |
| 147 | pld [r1, #0] |
| 148 | pld [r1, #64] |
| 149 | ldr r0, [sp] |
| 150 | cmp r3, lr |
Brent DeGraaf | 1d0268c | 2013-10-02 13:47:11 +0000 | [diff] [blame] | 151 | bhs .L_strcpy_chk_failed |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 152 | |
| 153 | // Add 1 for copy length to get the string terminator. |
| 154 | add r2, r3, #1 |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 155 | |
Christopher Ferris | a57c9c0 | 2013-08-21 09:41:12 -0700 | [diff] [blame] | 156 | #include "memcpy_base.S" |
| 157 | |
Brent DeGraaf | 1d0268c | 2013-10-02 13:47:11 +0000 | [diff] [blame] | 158 | .L_strcpy_chk_failed: |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 159 | ldr r0, error_message |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 160 | 1: |
| 161 | add r0, pc |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 162 | bl __fortify_fatal |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 163 | error_message: |
| 164 | .word error_string-(1b+4) |
Brent DeGraaf | 1d0268c | 2013-10-02 13:47:11 +0000 | [diff] [blame] | 165 | END(__strcpy_chk) |
Christopher Ferris | 5f45d58 | 2013-08-07 13:09:51 -0700 | [diff] [blame] | 166 | |
| 167 | .data |
| 168 | error_string: |
Elliott Hughes | 68b6711 | 2013-10-15 17:17:05 -0700 | [diff] [blame] | 169 | .string "strcpy: prevented write past end of buffer" |