| 1 | /*------------------------------------------------------------------ |
| 2 | * memcpy_s |
| 3 | * |
| 4 | * October 2008, Bo Berry |
| 5 | * |
| 6 | * Copyright (c) 2008-2011 Cisco Systems |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person |
| 10 | * obtaining a copy of this software and associated documentation |
| 11 | * files (the "Software"), to deal in the Software without |
| 12 | * restriction, including without limitation the rights to use, |
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | * sell copies of the Software, and to permit persons to whom the |
| 15 | * Software is furnished to do so, subject to the following |
| 16 | * conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | *------------------------------------------------------------------ |
| 30 | */ |
| 31 | |
| 32 | #include "safeclib_private.h" |
| 33 | #include "safe_mem_constraint.h" |
| 34 | #include "mem_primitives_lib.h" |
| 35 | #include "safe_mem_lib.h" |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * NAME |
| 40 | * memcpy_s |
| 41 | * |
| 42 | * SYNOPSIS |
| 43 | * #include "safe_mem_lib.h" |
| 44 | * errno_t |
| 45 | * memcpy_s(void *dest, rsize_t dmax, const void *src, rsize_t smax) |
| 46 | * |
| 47 | * DESCRIPTION |
| 48 | * This function copies at most smax bytes from src to dest, up to |
| 49 | * dmax. The size values are unsigned values. |
| 50 | * |
| 51 | * AR: Dave - verify ISO spec requires unsigned |
| 52 | * |
| 53 | * SPECIFIED IN |
| 54 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments |
| 55 | * and system software interfaces, Extensions to the C Library, |
| 56 | * Part I: Bounds-checking interfaces |
| 57 | * |
| 58 | * INPUT PARAMETERS |
| 59 | * dest pointer to memory that will be replaced by src. |
| 60 | * |
| 61 | * dmax maximum length of the resulting dest |
| 62 | * |
| 63 | * src pointer to the memory that will be copied to dest |
| 64 | * |
| 65 | * smax maximum number bytes of src to copy |
| 66 | * |
| 67 | * OUTPUT PARAMETERS |
| 68 | * dest is updated |
| 69 | * |
| 70 | * RUNTIME CONSTRAINTS |
| 71 | * Neither dest nor src shall be a null pointer. |
| 72 | * Neither dmax nor smax shall be zero. |
| 73 | * dmax shall not be greater than RSIZE_MAX_MEM. |
| 74 | * smax shall not be greater than dmax. |
| 75 | * Copying shall not take place between regions that overlap. |
| 76 | * If there is a runtime-constraint violation, the memcpy_s function |
| 77 | * stores zeros in the first dmax bytes of the region pointed to |
| 78 | * by dest if dest is not a null pointer and smax is valid. |
| 79 | * |
| 80 | * RETURN VALUE |
| 81 | * EOK successful operation |
| 82 | * ESNULLP NULL pointer |
| 83 | * ESZEROL zero length |
| 84 | * ESLEMAX length exceeds max limit |
| 85 | * ESOVRLP source memory overlaps destination |
| 86 | * |
| 87 | * ALSO SEE |
| 88 | * memcpy16_s(), memcpy32_s(), memmove_s(), memmove16_s(), |
| 89 | * memmove32_s() |
| 90 | * |
| 91 | */ |
| 92 | errno_t |
| 93 | memcpy_s (void *dest, rsize_t dmax, const void *src, rsize_t smax) |
| 94 | { |
| 95 | uint8_t *dp; |
| 96 | const uint8_t *sp; |
| 97 | |
| 98 | dp = dest; |
| 99 | sp = src; |
| 100 | |
| 101 | if (dp == NULL) { |
| 102 | invoke_safe_mem_constraint_handler("memcpy_s: dest is NULL", |
| 103 | NULL, ESNULLP); |
| 104 | return RCNEGATE(ESNULLP); |
| 105 | } |
| 106 | |
| 107 | if (dmax == 0) { |
| 108 | invoke_safe_mem_constraint_handler("memcpy_s: dmax is 0", |
| 109 | NULL, ESZEROL); |
| 110 | return RCNEGATE(ESZEROL); |
| 111 | } |
| 112 | |
| 113 | if (dmax > RSIZE_MAX_MEM) { |
| 114 | invoke_safe_mem_constraint_handler("memcpy_s: dmax exceeds max", |
| 115 | NULL, ESLEMAX); |
| 116 | return RCNEGATE(ESLEMAX); |
| 117 | } |
| 118 | |
| 119 | // AR: This is not a requirement according to the ISO spec - Change? |
| 120 | // AR: documentation needed on use of the error handlers - |
| 121 | // AR: default err handler should output to stderr on DEBUG |
| 122 | // AR: update docs to define return RCNEGATE of the error number |
| 123 | if (smax == 0) { |
| 124 | mem_prim_set(dp, dmax, 0); |
| 125 | invoke_safe_mem_constraint_handler("memcpy_s: smax is 0", |
| 126 | NULL, ESZEROL); |
| 127 | return RCNEGATE(ESZEROL); |
| 128 | } |
| 129 | |
| 130 | if (smax > dmax) { |
| 131 | mem_prim_set(dp, dmax, 0); |
| 132 | invoke_safe_mem_constraint_handler("memcpy_s: smax exceeds dmax", |
| 133 | NULL, ESLEMAX); |
| 134 | return RCNEGATE(ESLEMAX); |
| 135 | } |
| 136 | |
| 137 | if (sp == NULL) { |
| 138 | mem_prim_set(dp, dmax, 0); |
| 139 | invoke_safe_mem_constraint_handler("memcpy_s: src is NULL", |
| 140 | NULL, ESNULLP); |
| 141 | return RCNEGATE(ESNULLP); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /* |
| 146 | * overlap is undefined behavior, do not allow |
| 147 | */ |
| 148 | if( ((dp > sp) && (dp < (sp+smax))) || |
| 149 | ((sp > dp) && (sp < (dp+dmax))) ) { |
| 150 | mem_prim_set(dp, dmax, 0); |
| 151 | invoke_safe_mem_constraint_handler("memcpy_s: overlap undefined", |
| 152 | NULL, ESOVRLP); |
| 153 | return RCNEGATE(ESOVRLP); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * now perform the copy |
| 158 | */ |
| 159 | mem_prim_move(dp, sp, smax); |
| 160 | |
| 161 | return RCNEGATE(EOK); |
| 162 | } |
| 163 | EXPORT_SYMBOL(memcpy_s) |