Skip to content

Commit 2afd65d

Browse files
Zexin Lihackorum
authored andcommitted
Reject out-of-range octets in macaddr input
macaddr_in() parsed the colon- and dash-separated notations with unbounded sscanf %x conversions. A field with more than eight significant hex digits overflows the int variable, which the C standard leaves undefined; in practice the value wrapped, so the later 0..255 range check tested the wrapped result and passed. '100000001:0:0:0:0:0' was therefore accepted and stored as 01:00:00:00:00:00, an address other than the one given, and whether an overlong field was rejected at all depended on where the wrapped value happened to land. Parse the fields of those two notations with strtol() instead, which handles an out-of-range value in a defined way on every platform: it yields the exact value whenever that fits in a long, and LONG_MIN or LONG_MAX otherwise. Either way the existing octet range check rejects the field. This follows what from_char_parse_int_len() in formatting.c does, parsing with strtol() and range-checking the result. Because %x is defined in terms of strtoul()'s subject sequence, strtol() accepts the same field syntax, so every input that is accepted today and stores the correct value keeps working. That includes forms which are not documented but have been accepted since the type was added in 1998: zero-padded fields such as '001:00:2b:01:02:03', 0x-prefixed fields, signed fields, single-digit fields, and whitespace after a separator. Both error messages stay as they are. The one exception is a bare '0x' field with no hex digit after it: glibc's sscanf read it as zero, strtol() stops at the '0', and that acceptance already varied by platform. The colon and dash notations therefore no longer accept such fields. What happens to an input containing one is then up to the unchanged condensed templates: most such inputs draw the syntax error; on glibc, a few dash-separated ones still match a condensed template and either draw the octet error or stay accepted with the same stored value; and ones that used to fail the octet check draw the syntax error now. The five condensed notations are left alone, as their %2x conversions cannot overflow. Bug: #19583 Reported-by: Michael Malis <malis@pgrust.com> Discussion: https://postgr.es/m/19583-ca7c85d40164f18b@postgresql.org
1 parent 31878cc commit 2afd65d

3 files changed

Lines changed: 182 additions & 33 deletions

File tree

‎src/backend/utils/adt/mac.c‎

Lines changed: 87 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,51 @@ static int macaddr_cmp_internal(macaddr *a1, macaddr *a2);
3535
static int macaddr_fast_cmp(Datum x, Datum y, SortSupport ssup);
3636
static bool macaddr_abbrev_abort(int memtupcount, SortSupport ssup);
3737
static Datum macaddr_abbrev_convert(Datum original, SortSupport ssup);
38+
static bool macaddr_parse_octets(const char *str, char separator, long *octets);
39+
40+
/*
41+
* Parse the six octets of one of the separator-based notations.
42+
*
43+
* strtol() accepts the same field syntax as the %x conversions this
44+
* replaces, but has defined behavior when a field is out of range: it
45+
* yields the exact value whenever that fits in a long, and LONG_MIN or
46+
* LONG_MAX otherwise. Either way the caller's 0..255 range check
47+
* rejects the field, so no errno test is needed here. One divergence:
48+
* strtol() stops at the "0" of a bare "0x" with no hex digit after it,
49+
* which glibc's sscanf read as zero (that varied across platforms).
50+
*
51+
* Returns true if str matched this notation, false if it did not, in
52+
* which case the caller falls through to the remaining notations.
53+
*/
54+
static bool
55+
macaddr_parse_octets(const char *str, char separator, long *octets)
56+
{
57+
const char *ptr = str;
58+
int i;
59+
60+
for (i = 0; i < 6; i++)
61+
{
62+
char *endptr;
63+
64+
octets[i] = strtol(ptr, &endptr, 16);
65+
if (endptr == ptr)
66+
return false; /* no digits where an octet was expected */
67+
ptr = endptr;
68+
69+
if (i < 5)
70+
{
71+
if (*ptr != separator)
72+
return false;
73+
ptr++;
74+
}
75+
}
76+
77+
/* trailing whitespace is accepted, anything else is garbage */
78+
while (isspace((unsigned char) *ptr))
79+
ptr++;
80+
81+
return (*ptr == '\0');
82+
}
3883

3984
/*
4085
* MAC address reader. Accepts several common notations.
@@ -46,6 +91,7 @@ macaddr_in(PG_FUNCTION_ARGS)
4691
char *str = PG_GETARG_CSTRING(0);
4792
Node *escontext = fcinfo->context;
4893
macaddr *result;
94+
long octets[6];
4995
int a,
5096
b,
5197
c,
@@ -55,49 +101,57 @@ macaddr_in(PG_FUNCTION_ARGS)
55101
char junk[2];
56102
int count;
57103

58-
/* %1s matches iff there is trailing non-whitespace garbage */
104+
if (!macaddr_parse_octets(str, ':', octets) &&
105+
!macaddr_parse_octets(str, '-', octets))
106+
{
107+
/* %1s matches iff there is trailing non-whitespace garbage */
59108

60-
count = sscanf(str, "%x:%x:%x:%x:%x:%x%1s",
61-
&a, &b, &c, &d, &e, &f, junk);
62-
if (count != 6)
63-
count = sscanf(str, "%x-%x-%x-%x-%x-%x%1s",
64-
&a, &b, &c, &d, &e, &f, junk);
65-
if (count != 6)
66109
count = sscanf(str, "%2x%2x%2x:%2x%2x%2x%1s",
67110
&a, &b, &c, &d, &e, &f, junk);
68-
if (count != 6)
69-
count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s",
70-
&a, &b, &c, &d, &e, &f, junk);
71-
if (count != 6)
72-
count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s",
73-
&a, &b, &c, &d, &e, &f, junk);
74-
if (count != 6)
75-
count = sscanf(str, "%2x%2x-%2x%2x-%2x%2x%1s",
76-
&a, &b, &c, &d, &e, &f, junk);
77-
if (count != 6)
78-
count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s",
79-
&a, &b, &c, &d, &e, &f, junk);
80-
if (count != 6)
81-
ereturn(escontext, (Datum) 0,
82-
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
83-
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr",
84-
str)));
111+
if (count != 6)
112+
count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s",
113+
&a, &b, &c, &d, &e, &f, junk);
114+
if (count != 6)
115+
count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s",
116+
&a, &b, &c, &d, &e, &f, junk);
117+
if (count != 6)
118+
count = sscanf(str, "%2x%2x-%2x%2x-%2x%2x%1s",
119+
&a, &b, &c, &d, &e, &f, junk);
120+
if (count != 6)
121+
count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s",
122+
&a, &b, &c, &d, &e, &f, junk);
123+
if (count != 6)
124+
ereturn(escontext, (Datum) 0,
125+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
126+
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr",
127+
str)));
128+
129+
octets[0] = a;
130+
octets[1] = b;
131+
octets[2] = c;
132+
octets[3] = d;
133+
octets[4] = e;
134+
octets[5] = f;
135+
}
85136

86-
if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
87-
(c < 0) || (c > 255) || (d < 0) || (d > 255) ||
88-
(e < 0) || (e > 255) || (f < 0) || (f > 255))
137+
if ((octets[0] < 0) || (octets[0] > 255) ||
138+
(octets[1] < 0) || (octets[1] > 255) ||
139+
(octets[2] < 0) || (octets[2] > 255) ||
140+
(octets[3] < 0) || (octets[3] > 255) ||
141+
(octets[4] < 0) || (octets[4] > 255) ||
142+
(octets[5] < 0) || (octets[5] > 255))
89143
ereturn(escontext, (Datum) 0,
90144
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
91145
errmsg("invalid octet value in \"macaddr\" value: \"%s\"", str)));
92146

93147
result = palloc_object(macaddr);
94148

95-
result->a = a;
96-
result->b = b;
97-
result->c = c;
98-
result->d = d;
99-
result->e = e;
100-
result->f = f;
149+
result->a = octets[0];
150+
result->b = octets[1];
151+
result->c = octets[2];
152+
result->d = octets[3];
153+
result->e = octets[4];
154+
result->f = octets[5];
101155

102156
PG_RETURN_MACADDR_P(result);
103157
}

‎src/test/regress/expected/macaddr.out‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,68 @@ INSERT INTO macaddr_data VALUES (9, 'not even close'); -- invalid
1717
ERROR: invalid input syntax for type macaddr: "not even close"
1818
LINE 1: INSERT INTO macaddr_data VALUES (9, 'not even close');
1919
^
20+
-- Overlong hex fields must be rejected, not silently wrapped (bug #19583)
21+
SELECT '100000001:0:0:0:0:0'::macaddr;
22+
ERROR: invalid octet value in "macaddr" value: "100000001:0:0:0:0:0"
23+
LINE 1: SELECT '100000001:0:0:0:0:0'::macaddr;
24+
^
25+
SELECT '100000001-0-0-0-0-0'::macaddr;
26+
ERROR: invalid octet value in "macaddr" value: "100000001-0-0-0-0-0"
27+
LINE 1: SELECT '100000001-0-0-0-0-0'::macaddr;
28+
^
29+
SELECT '-ffffff01:0:0:0:0:0'::macaddr;
30+
ERROR: invalid octet value in "macaddr" value: "-ffffff01:0:0:0:0:0"
31+
LINE 1: SELECT '-ffffff01:0:0:0:0:0'::macaddr;
32+
^
33+
SELECT 'ffffffffffffffffff:0:0:0:0:0'::macaddr; -- wider than any long
34+
ERROR: invalid octet value in "macaddr" value: "ffffffffffffffffff:0:0:0:0:0"
35+
LINE 1: SELECT 'ffffffffffffffffff:0:0:0:0:0'::macaddr;
36+
^
37+
-- while the leniency inherited from sscanf's %x conversion is preserved
38+
SELECT '001:00:2b:01:02:03'::macaddr;
39+
macaddr
40+
-------------------
41+
01:00:2b:01:02:03
42+
(1 row)
43+
44+
SELECT '0x1:00:2b:01:02:03'::macaddr;
45+
macaddr
46+
-------------------
47+
01:00:2b:01:02:03
48+
(1 row)
49+
50+
SELECT '+f:00:2b:01:02:03'::macaddr;
51+
macaddr
52+
-------------------
53+
0f:00:2b:01:02:03
54+
(1 row)
55+
56+
SELECT 'aa: bb:cc:dd:ee:ff'::macaddr;
57+
macaddr
58+
-------------------
59+
aa:bb:cc:dd:ee:ff
60+
(1 row)
61+
62+
SELECT '08:00:2b:01:02:03 '::macaddr;
63+
macaddr
64+
-------------------
65+
08:00:2b:01:02:03
66+
(1 row)
67+
68+
-- but a bare '0x' field with no hex digit is not valid
69+
SELECT '0x:00:2b:01:02:03'::macaddr;
70+
ERROR: invalid input syntax for type macaddr: "0x:00:2b:01:02:03"
71+
LINE 1: SELECT '0x:00:2b:01:02:03'::macaddr;
72+
^
73+
-- an octet that fits the field width but is out of range is still caught
74+
SELECT '1ff:0:0:0:0:0'::macaddr;
75+
ERROR: invalid octet value in "macaddr" value: "1ff:0:0:0:0:0"
76+
LINE 1: SELECT '1ff:0:0:0:0:0'::macaddr;
77+
^
78+
SELECT '-f:0:0:0:0:0'::macaddr;
79+
ERROR: invalid octet value in "macaddr" value: "-f:0:0:0:0:0"
80+
LINE 1: SELECT '-f:0:0:0:0:0'::macaddr;
81+
^
2082
INSERT INTO macaddr_data VALUES (10, '08:00:2b:01:02:04');
2183
INSERT INTO macaddr_data VALUES (11, '08:00:2b:01:02:02');
2284
INSERT INTO macaddr_data VALUES (12, '08:00:2a:01:02:03');
@@ -183,3 +245,16 @@ SELECT * FROM pg_input_error_info('08:00:2b:01:02:', 'macaddr');
183245
invalid input syntax for type macaddr: "08:00:2b:01:02:" | | | 22P02
184246
(1 row)
185247

248+
-- overlong hex field (bug #19583)
249+
SELECT pg_input_is_valid('100000001:0:0:0:0:0', 'macaddr');
250+
pg_input_is_valid
251+
-------------------
252+
f
253+
(1 row)
254+
255+
SELECT * FROM pg_input_error_info('100000001:0:0:0:0:0', 'macaddr');
256+
message | detail | hint | sql_error_code
257+
---------------------------------------------------------------+--------+------+----------------
258+
invalid octet value in "macaddr" value: "100000001:0:0:0:0:0" | | | 22003
259+
(1 row)
260+

‎src/test/regress/sql/macaddr.sql‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ INSERT INTO macaddr_data VALUES (7, '08002b010203');
1414
INSERT INTO macaddr_data VALUES (8, '0800:2b01:0203'); -- invalid
1515
INSERT INTO macaddr_data VALUES (9, 'not even close'); -- invalid
1616

17+
-- Overlong hex fields must be rejected, not silently wrapped (bug #19583)
18+
SELECT '100000001:0:0:0:0:0'::macaddr;
19+
SELECT '100000001-0-0-0-0-0'::macaddr;
20+
SELECT '-ffffff01:0:0:0:0:0'::macaddr;
21+
SELECT 'ffffffffffffffffff:0:0:0:0:0'::macaddr; -- wider than any long
22+
-- while the leniency inherited from sscanf's %x conversion is preserved
23+
SELECT '001:00:2b:01:02:03'::macaddr;
24+
SELECT '0x1:00:2b:01:02:03'::macaddr;
25+
SELECT '+f:00:2b:01:02:03'::macaddr;
26+
SELECT 'aa: bb:cc:dd:ee:ff'::macaddr;
27+
SELECT '08:00:2b:01:02:03 '::macaddr;
28+
-- but a bare '0x' field with no hex digit is not valid
29+
SELECT '0x:00:2b:01:02:03'::macaddr;
30+
-- an octet that fits the field width but is out of range is still caught
31+
SELECT '1ff:0:0:0:0:0'::macaddr;
32+
SELECT '-f:0:0:0:0:0'::macaddr;
33+
1734
INSERT INTO macaddr_data VALUES (10, '08:00:2b:01:02:04');
1835
INSERT INTO macaddr_data VALUES (11, '08:00:2b:01:02:02');
1936
INSERT INTO macaddr_data VALUES (12, '08:00:2a:01:02:03');
@@ -47,3 +64,6 @@ SELECT pg_input_is_valid('08:00:2b:01:02:ZZ', 'macaddr');
4764
SELECT * FROM pg_input_error_info('08:00:2b:01:02:ZZ', 'macaddr');
4865
SELECT pg_input_is_valid('08:00:2b:01:02:', 'macaddr');
4966
SELECT * FROM pg_input_error_info('08:00:2b:01:02:', 'macaddr');
67+
-- overlong hex field (bug #19583)
68+
SELECT pg_input_is_valid('100000001:0:0:0:0:0', 'macaddr');
69+
SELECT * FROM pg_input_error_info('100000001:0:0:0:0:0', 'macaddr');

0 commit comments

Comments
 (0)