Some pf ioctl handlers use strlcpy() to copy strings when converting
from user structures to their in-kernel representations. strlcpy()
ensures that the destination will be nul-terminated, but it assumes that
the source is nul-terminated. In particular, it returns the full length
of the source string, so if the source is not nul-terminated, strlcpy()
will keep scanning until it finds a nul byte, and it may encounter an
unmapped page first.
Add a helper to validate user strings and use it in ioctl handlers which
use strlcpy() to copy user-provided strings.
There are also many places where we look up a ruleset using a
user-provided anchor string. In some ioctl handlers we were already
nul-terminating the string, avoiding the same problem, but in other
places we were not. Fix those by nul-terminating as well. Aside from
being consistent, anchors have a maximum length of MAXPATHLEN - 1 so
calling strnlen() might not be so desirable, though I imagine that
anchors are usually short in practice.
Reported by: syzbot+35a1549b4663e9483dd1@syzkaller.appspotmail.com