Skip to content

Commit 90862fe

Browse files
committed
Fix GH-21336: undefined behavior in snmp setSecurity.
1 parent 583be5a commit 90862fe

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

ext/snmp/snmp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,10 @@ static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *l
961961
/* {{{ Set the authentication protocol in the snmpv3 session */
962962
static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
963963
{
964+
if (!prot) {
965+
zend_value_error("Authentication protocol can't be NULL");
966+
return false;
967+
}
964968
#ifndef DISABLE_MD5
965969
if (zend_string_equals_literal_ci(prot, "MD5")) {
966970
s->securityAuthProto = usmHMACMD5AuthProtocol;
@@ -1013,6 +1017,10 @@ static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_strin
10131017
/* {{{ Set the security protocol in the snmpv3 session */
10141018
static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
10151019
{
1020+
if (!prot) {
1021+
zend_value_error("Security protocol can't be NULL");
1022+
return false;
1023+
}
10161024
#ifndef NETSNMP_DISABLE_DES
10171025
if (zend_string_equals_literal_ci(prot, "DES")) {
10181026
s->securityPrivProto = usmDESPrivProtocol;
@@ -1051,6 +1059,12 @@ static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string
10511059
static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
10521060
{
10531061
int snmp_errno;
1062+
1063+
if (!pass) {
1064+
zend_value_error("Authentication key can't be NULL");
1065+
return false;
1066+
}
1067+
10541068
s->securityAuthKeyLen = USM_AUTH_KU_LEN;
10551069
if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
10561070
(uint8_t *) ZSTR_VAL(pass), ZSTR_LEN(pass),
@@ -1067,6 +1081,11 @@ static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pas
10671081
{
10681082
int snmp_errno;
10691083

1084+
if (!pass) {
1085+
zend_value_error("Security key can't be NULL");
1086+
return false;
1087+
}
1088+
10701089
s->securityPrivKeyLen = USM_PRIV_KU_LEN;
10711090
if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
10721091
(uint8_t *)ZSTR_VAL(pass), ZSTR_LEN(pass),

ext/snmp/tests/gh21336.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
GH-21336 (undefined behavior in snmp - NULL pointer dereference in setSecurity)
3+
--EXTENSIONS--
4+
snmp
5+
--FILE--
6+
<?php
7+
$session = new SNMP(SNMP::VERSION_3, 'localhost', 'user');
8+
9+
// auth protocol NULL
10+
try {
11+
$session->setSecurity('authPriv');
12+
} catch (ValueError $e) {
13+
echo $e->getMessage() . PHP_EOL;
14+
}
15+
16+
// auth passphrase NULL
17+
try {
18+
$session->setSecurity('authNoPriv', 'MD5');
19+
} catch (ValueError $e) {
20+
echo $e->getMessage() . PHP_EOL;
21+
}
22+
23+
// priv protocol NULL
24+
try {
25+
$session->setSecurity('authPriv', 'MD5', 'test12345');
26+
} catch (ValueError $e) {
27+
echo $e->getMessage() . PHP_EOL;
28+
}
29+
30+
// priv passphrase NULL
31+
try {
32+
$session->setSecurity('authPriv', 'MD5', 'test12345', 'AES');
33+
} catch (ValueError $e) {
34+
echo $e->getMessage() . PHP_EOL;
35+
}
36+
?>
37+
--EXPECT--
38+
Authentication protocol can't be NULL
39+
Authentication key can't be NULL
40+
Security protocol can't be NULL
41+
Security key can't be NULL

0 commit comments

Comments
 (0)