sysctl: Harden sysctl_handle_string() against unterminated string
In case a variable string which is not null-terminated is passed in,
strlen() may report a length exceeding the max length, hence it is
possible to leak a portion of kernel memory to the userland.
Harden that by using strnlen() to limit the length to the max length.
While here, refactor the code a little to improve readability.
Note that, when calculating the out length, the null terminator '\0' of
the string is taken into account if available. This is not really
necessary but userland applications may have already relied on this
behavior.
Reviewed by: avg, kib, olce
Fixes: 210176ad76ee sysctl(9): add CTLFLAG_NEEDGIANT flag
MFC after: 4 days
Differential Revision: https://reviews.freebsd.org/D48881