pfsense/pfsense — pull request #973 by stilez (2014-02-25 15:05:49+00:00)
Title: Tighten is_linklocal()

is_linklocal has a few issues, including validating as linklocal, addresses that aren't linklocal according to RFC 4291, and doing no validation of reasonableness on any %(interface) part that may exist, allowing this to contain arbitrary text.

Changes made:

1) Tests for IPv4 linklocal addresses - while not much needed, they should probably be recognised as code handling linklocal may reasonably expect is_linklocal() to be IPv4/IPv6 agnostic.

2) For IPv6, it tests at least, that the purported interface is [0-9a-z]+ otherwise user input or other text such as "fe80::%\n;ARBIRARYTEXT;" would be validated as a linklocal address and inserted into pf and perhaps other places without further detection, leading to possible vulnerabilities. But it doesn't test more than this (and probably should test for valid interface name).

3) Follows RFC 4291 exactly: IPv6 linklocal isn't just "fe80::", it requires the rest of the first 64 bits to be zero too. The RFC defines it as '1111111010' + 54 zeros (Ref: https://tools.ietf.org/html/rfc4291#section-2.5.6 )

4) Returns 4 or 6 to give a more exact response to the calling function as to whether the match was an IPv4 linklocal or IPv6 linklocal address (both evaluate to True for Boolean test purposes such as "if (is_linklocal(...))")