aboutsummaryrefslogtreecommitdiff
path: root/lib/std/net.zig
diff options
context:
space:
mode:
authorFrank Denis <124872+jedisct1@users.noreply.github.com>2021-08-09 22:44:23 +0200
committerGitHub <noreply@github.com>2021-08-09 22:44:23 +0200
commit2ccd023c6ae590b4ff311814ccf5ff508c7669ef (patch)
tree6d3e420271225b5e5d22bf8f1d1e9666d8eb01e8 /lib/std/net.zig
parent799fedf612aa8742c446b015c12d21707a1dbec0 (diff)
downloadzig-2ccd023c6ae590b4ff311814ccf5ff508c7669ef.tar.gz
zig-2ccd023c6ae590b4ff311814ccf5ff508c7669ef.zip
Ip4Address parser: reject 0-prefixed components (#9538)
Some parsers interpret these as octal, some don't, and the confusion can lead to vulnerabilities. Return error.NonCanonical when parsing IPv4 addresses with 0 prefixes.
Diffstat (limited to 'lib/std/net.zig')
-rw-r--r--lib/std/net.zig10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig
index 1b53399fd1..fea033dc9c 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -34,6 +34,7 @@ pub const Address = extern union {
error.InvalidEnd,
error.InvalidCharacter,
error.Incomplete,
+ error.NonCanonical,
=> {},
}
@@ -55,6 +56,7 @@ pub const Address = extern union {
error.InvalidEnd,
error.InvalidCharacter,
error.Incomplete,
+ error.NonCanonical,
=> {},
}
@@ -204,6 +206,7 @@ pub const Ip4Address = extern struct {
var x: u8 = 0;
var index: u8 = 0;
var saw_any_digits = false;
+ var has_zero_prefix = false;
for (buf) |c| {
if (c == '.') {
if (!saw_any_digits) {
@@ -216,7 +219,13 @@ pub const Ip4Address = extern struct {
index += 1;
x = 0;
saw_any_digits = false;
+ has_zero_prefix = false;
} else if (c >= '0' and c <= '9') {
+ if (c == '0' and !saw_any_digits) {
+ has_zero_prefix = true;
+ } else if (has_zero_prefix) {
+ return error.NonCanonical;
+ }
saw_any_digits = true;
x = try std.math.mul(u8, x, 10);
x = try std.math.add(u8, x, c - '0');
@@ -1149,6 +1158,7 @@ fn linuxLookupNameFromHosts(
error.Incomplete,
error.InvalidIPAddressFormat,
error.InvalidIpv4Mapping,
+ error.NonCanonical,
=> continue,
};
try addrs.append(LookupAddr{ .addr = addr });