diff options
| author | cheloha <> | 2021-12-07 04:01:45 +0000 |
|---|---|---|
| committer | cheloha <> | 2021-12-07 04:01:45 +0000 |
| commit | cfc78852fcba382d04261ab49ed38b22e811e417 (patch) | |
| tree | fb01e229aaae158bfbe4d7b52b61fa7fc95e523c /src/lib/libc | |
| parent | 036164e800dded700d72e343b440ebe3a04af264 (diff) | |
| download | openbsd-cfc78852fcba382d04261ab49ed38b22e811e417.tar.gz openbsd-cfc78852fcba382d04261ab49ed38b22e811e417.tar.bz2 openbsd-cfc78852fcba382d04261ab49ed38b22e811e417.zip | |
lsearch(3): append key to array with memmove(3) instead of memcpy(3)
If the key overlaps the end of the array, memcpy(3) mutates the key
and copies a corrupted value into the end of the array.
If we use memmove(3) instead we at least end up with a clean copy of
the key at the end of the array. This is closer to the intended
behavior.
With input from millert@ and deraadt@.
Thread: https://marc.info/?l=openbsd-tech&m=163880307403606&w=2
ok millert@
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/stdlib/lsearch.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c index 8cad05f510..93e200e1bd 100644 --- a/src/lib/libc/stdlib/lsearch.c +++ b/src/lib/libc/stdlib/lsearch.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: lsearch.c,v 1.5 2014/07/18 04:16:09 matthew Exp $ */ | 1 | /* $OpenBSD: lsearch.c,v 1.6 2021/12/07 04:01:45 cheloha Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1989, 1993 | 4 | * Copyright (c) 1989, 1993 |
| @@ -79,6 +79,11 @@ linear_base(const void *key, const void *base, size_t *nelp, size_t width, | |||
| 79 | * manual. | 79 | * manual. |
| 80 | */ | 80 | */ |
| 81 | ++*nelp; | 81 | ++*nelp; |
| 82 | memcpy((void *)end, key, width); | 82 | |
| 83 | /* | ||
| 84 | * Use memmove(3) to ensure the key is copied cleanly into the | ||
| 85 | * array, even if the key overlaps with the end of the array. | ||
| 86 | */ | ||
| 87 | memmove((void *)end, key, width); | ||
| 83 | return((void *)end); | 88 | return((void *)end); |
| 84 | } | 89 | } |
