summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcheloha <>2021-12-07 04:01:45 +0000
committercheloha <>2021-12-07 04:01:45 +0000
commit0dac27b8f62da9acb2557baf3463eaa689bc0332 (patch)
treefb01e229aaae158bfbe4d7b52b61fa7fc95e523c
parent1899d459607a5a561a64371d6a7eecdd4407fc43 (diff)
downloadopenbsd-0dac27b8f62da9acb2557baf3463eaa689bc0332.tar.gz
openbsd-0dac27b8f62da9acb2557baf3463eaa689bc0332.tar.bz2
openbsd-0dac27b8f62da9acb2557baf3463eaa689bc0332.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@
-rw-r--r--src/lib/libc/stdlib/lsearch.c9
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}