Skip to content

Commit edb4b9e

Browse files
Fix GH-25137: Reject malformed SNMP ports
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
1 parent 7091e5b commit edb4b9e

3 files changed

Lines changed: 85 additions & 7 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ PHP NEWS
723723
TAR archive entry injection). (CVE-2026-6103) (Jakub Zelenka)
724724

725725
- SNMP:
726+
. Fixed bug GH-25137 (SNMP accepts malformed remote ports).
727+
(somethingwithproof)
726728
. Fixed bug GH-23453 (SNMP::setSecurity() frees a non-malloced address with a
727729
context engine ID longer than 32 bytes). (Lazizbek Ergashev)
728730

‎ext/snmp/snmp.c‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,33 @@ static bool php_snmp_parse_oid(
829829
}
830830
/* }}} */
831831

832+
static bool php_snmp_parse_port(const char *port_string, unsigned short *port)
833+
{
834+
unsigned int value = 0;
835+
836+
if (*port_string == '\0') {
837+
return false;
838+
}
839+
840+
do {
841+
if (!ZEND_IS_DIGIT(*port_string)) {
842+
return false;
843+
}
844+
845+
value = value * 10 + (unsigned int) (*port_string - '0');
846+
if (value > USHRT_MAX) {
847+
return false;
848+
}
849+
} while (*++port_string != '\0');
850+
851+
*port = (unsigned short) value;
852+
return true;
853+
}
854+
832855
/* {{{ netsnmp_session_init
833856
allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
834857
*/
835-
static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, int timeout, int retries)
858+
static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, int timeout, int retries, uint32_t hostname_arg_num)
836859
{
837860
php_snmp_session *session;
838861
char *pptr, *host_ptr;
@@ -841,7 +864,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
841864
struct sockaddr **psal;
842865
struct sockaddr **res;
843866
// TODO: Do not strip and re-add the port in peername?
844-
unsigned remote_port = SNMP_PORT;
867+
unsigned short remote_port = SNMP_PORT;
845868

846869
*session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
847870
session = *session_p;
@@ -862,7 +885,10 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
862885
host_ptr++;
863886
if ((pptr = strchr(host_ptr, ']'))) {
864887
if (pptr[1] == ':') {
865-
remote_port = atoi(pptr + 2);
888+
if (!php_snmp_parse_port(pptr + 2, &remote_port)) {
889+
zend_argument_value_error(hostname_arg_num, "remote port must be between 0 and %u", (unsigned int) USHRT_MAX);
890+
return false;
891+
}
866892
}
867893
*pptr = '\0';
868894
} else {
@@ -871,7 +897,10 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
871897
}
872898
} else { /* IPv4 address */
873899
if ((pptr = strchr(host_ptr, ':'))) {
874-
remote_port = atoi(pptr + 1);
900+
if (!php_snmp_parse_port(pptr + 1, &remote_port)) {
901+
zend_argument_value_error(hostname_arg_num, "remote port must be between 0 and %u", (unsigned int) USHRT_MAX);
902+
return false;
903+
}
875904
*pptr = '\0';
876905
}
877906
}
@@ -921,7 +950,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
921950
if (remote_port != SNMP_PORT) {
922951
size_t peername_length = strlen(session->peername);
923952
pptr = session->peername + peername_length;
924-
snprintf(pptr, MAX_NAME_LEN - peername_length, ":%d", remote_port);
953+
snprintf(pptr, MAX_NAME_LEN - peername_length, ":%u", (unsigned int) remote_port);
925954
}
926955

927956
php_network_freeaddresses(psal);
@@ -1314,7 +1343,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
13141343
}
13151344

13161345
if (session_less_mode) {
1317-
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1346+
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries, 1)) {
13181347
php_free_objid_query(&objid_query, oid_ht, value_ht, st);
13191348
netsnmp_session_free(&session);
13201349
RETURN_FALSE;
@@ -1618,7 +1647,7 @@ PHP_METHOD(SNMP, __construct)
16181647
netsnmp_session_free(&(snmp_object->session));
16191648
}
16201649

1621-
if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1650+
if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries, 2)) {
16221651
return;
16231652
}
16241653
snmp_object->max_oids = 0;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
SNMP rejects malformed remote ports
3+
--EXTENSIONS--
4+
snmp
5+
--FILE--
6+
<?php
7+
8+
$hostnames = [
9+
'127.0.0.1:',
10+
'127.0.0.1:not-a-port',
11+
'127.0.0.1:123junk',
12+
'127.0.0.1:-1',
13+
'127.0.0.1:65536',
14+
'[::1]:',
15+
'[::1]:123junk',
16+
];
17+
18+
foreach ($hostnames as $hostname) {
19+
try {
20+
new SNMP(SNMP::VERSION_1, $hostname, 'public');
21+
} catch (Throwable $e) {
22+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
23+
}
24+
}
25+
26+
foreach (['127.0.0.1:0', '127.0.0.1:65535'] as $hostname) {
27+
var_dump(new SNMP(SNMP::VERSION_1, $hostname, 'public') instanceof SNMP);
28+
}
29+
30+
try {
31+
snmpget('127.0.0.1:not-a-port', 'public', '1.3.6.1.2.1.1.1.0');
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
35+
36+
?>
37+
--EXPECT--
38+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
39+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
40+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
41+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
42+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
43+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
44+
ValueError: SNMP::__construct(): Argument #2 ($hostname) remote port must be between 0 and 65535
45+
bool(true)
46+
bool(true)
47+
ValueError: snmpget(): Argument #1 ($hostname) remote port must be between 0 and 65535

0 commit comments

Comments
 (0)