Skip to content

Commit 73586fc

Browse files
committed
libkern: Avoid a one-byte OOB access in strndup()
If the length of the string is maxlen, we would end up copying maxlen+1 bytes, which violates the contract of the function. The result is the same since that extra byte is overwritten. Reported by: Kevin Day <[email protected]> Reviewed by: imp, kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D54093
1 parent 7922216 commit 73586fc

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

sys/libkern/strndup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ strndup(const char *string, size_t maxlen, struct malloc_type *type)
4040
size_t len;
4141
char *copy;
4242

43-
len = strnlen(string, maxlen) + 1;
44-
copy = malloc(len, type, M_WAITOK);
43+
len = strnlen(string, maxlen);
44+
copy = malloc(len + 1, type, M_WAITOK);
4545
memcpy(copy, string, len);
46-
copy[len - 1] = '\0';
46+
copy[len] = '\0';
4747
return (copy);
4848
}

0 commit comments

Comments
 (0)